Skip to content

feat: azion api v4 #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: stage
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ const config = {
entry: './src/index.js',
preset: 'react',
polyfills: true,
worker: true,
},
domain: {
name: 'example.com',
Expand Down Expand Up @@ -302,7 +301,6 @@ const config = defineConfig({
},
},
polyfills: true,
worker: true,
extend: (config) => {
// Customize bundler configuration
return {
Expand Down Expand Up @@ -394,7 +392,6 @@ export default defineConfig({
// Other build configurations
entry: './src/index.ts',
polyfills: true,
worker: true,
memoryFS: {
injectionDirs: ['./src/inject'],
removePathPrefix: './src',
Expand Down
8 changes: 3 additions & 5 deletions packages/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This module provides a function to configure and validate options for the Azion
- [`AzionDomain`](#aziondomain)
- [`AzionOrigin`](#azionorigin)
- [`AzionCache`](#azioncache)
- [`AzionRequestRule`](#azionrequestrule)
- [`AzionRuntimeRequestRule`](#AzionRuntimeRequestrule)
- [`AzionResponseRule`](#azionresponserule)
- [`AzionRules`](#azionrules)
- [`AzionPurge`](#azionpurge)
Expand Down Expand Up @@ -313,7 +313,6 @@ Type definition for the build configuration.
- `preset?: string | AzionBuildPreset` - The preset to be used, can be a string or an AzionBuildPreset object.
- `entry?: string | string[] | Record<string, string>` - The entry file, can be a string, an array of strings, or an object.
- `polyfills?: boolean` - Whether to include polyfills.
- `worker?: boolean` - Whether to build a worker.
- `extend?: (context: T) => T` - Function to extend the bundler configuration.
- `memoryFS?: { injectionDirs: string[], removePathPrefix: string }` - In-memory file system configuration.

Expand Down Expand Up @@ -371,7 +370,6 @@ Type definition for the build configuration.
- `setup: BundlerSetup` - Bundler configuration.
- \*`bundler?: 'webpack' | 'esbuild'` - The bundler to be used.
- `polyfills?: boolean` - Whether to include polyfills.
- `worker?: boolean` - Whether to build a worker.
- `extend?: (context: T) => T` - Function to extend the bundler configuration.
- `memoryFS?: { injectionDirs: string[], removePathPrefix: string }` - In-memory file system configuration.

Expand Down Expand Up @@ -458,7 +456,7 @@ Type definition for the cache configuration.
- `option: 'ignore' | 'varies' | 'whitelist' | 'blacklist'` - Cache by query string option.
- `list?: string[]` - List of query string parameters to use for caching.

### `AzionRequestRule`
### `AzionRuntimeRequestRule`

Type definition for the request rule configuration.

Expand Down Expand Up @@ -514,7 +512,7 @@ Type definition for the response rule configuration.

**Properties:**

- `request: AzionRequestRule[]` - Ruleset for Request phase.
- `request: AzionRuntimeRequestRule[]` - Ruleset for Request phase.
- `response?: AzionResponseRule[]` - Ruleset for Response phase.

### `AzionPurge`
Expand Down
6 changes: 1 addition & 5 deletions packages/config/src/configProcessor/helpers/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,10 +576,6 @@ const azionConfigSchema = {
type: 'boolean',
errorMessage: "The 'build.polyfills' must be a boolean",
},
worker: {
type: 'boolean',
errorMessage: "The 'build.worker' must be a boolean",
},
extend: {
instanceof: 'Function',
errorMessage: "The 'build.extend' must be a function",
Expand Down Expand Up @@ -1687,7 +1683,7 @@ const azionConfigSchema = {
additionalProperties: false,
errorMessage: {
additionalProperties:
'Config can only contain the following properties: build, edgeFunctions, edgeApplications, workloads, purge, edgefirewall, networkList, waf, edgeConnectors',
'Config can only contain the following properties: build, edgeFunctions, edgeFirewall, edgeApplications, edgeConnectors, edgeStorage, workloads, purge, networkList and waf',
type: 'Configuration must be an object',
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { processConfig } from '..';
import { AzionConfig } from '../../config/types';
import { AzionConfig } from '../../types';

describe('processConfig', () => {
describe('Cache and Rules', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EdgeApplicationProcessConfigStrategy extends ProcessConfigStrategy {
active: app.active ?? true,
debug: app.debug ?? false,
modules: {
edge_cache_enabled: app.edgeCacheEnabled ?? true,
edge_cache_enabled: app.edgeCacheEnabled ?? false,
edge_functions_enabled: app.edgeFunctionsEnabled ?? false,
application_accelerator_enabled: app.applicationAcceleratorEnabled ?? false,
image_processor_enabled: app.imageProcessorEnabled ?? false,
Expand Down Expand Up @@ -52,15 +52,20 @@ class EdgeApplicationProcessConfigStrategy extends ProcessConfigStrategy {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
transformedPayload.edgeApplications = payload.edgeApplications.map((app: any) => {
// Handle both formats: direct properties or nested in modules
const modules = app.modules || app;

return {
name: app.name,
active: app.active,
debug: app.debug,
edgeCacheEnabled: app.edge_cache_enabled,
edgeFunctionsEnabled: app.edge_functions_enabled,
applicationAcceleratorEnabled: app.application_accelerator_enabled,
imageProcessorEnabled: app.image_processor_enabled,
tieredCacheEnabled: app.tiered_cache_enabled,
modules: {
edgeCacheEnabled: modules.edge_cache_enabled ?? false,
edgeFunctionsEnabled: modules.edge_functions_enabled ?? false,
applicationAcceleratorEnabled: modules.application_accelerator_enabled ?? false,
imageProcessorEnabled: modules.image_processor_enabled ?? false,
tieredCacheEnabled: modules.tiered_cache_enabled ?? false,
},
cache: app.cache_settings ? this.cacheStrategy.transformToConfig(app.cache_settings) : undefined,
rules: app.rules ? this.rulesStrategy.transformToConfig(app.rules, transformedPayload) : undefined,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class StorageProcessConfigStrategy extends ProcessConfigStrategy {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
transformToConfig(payload: any, transformedPayload: AzionConfig) {
const storageConfig = payload.storage;
const storageConfig = payload.edgeStorage;
if (!Array.isArray(storageConfig) || storageConfig.length === 0) {
return;
}
Expand Down
9 changes: 4 additions & 5 deletions packages/config/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
WorkloadTLSVersion,
} from './constants';

import { FetchEvent } from 'azion/types';
import { AzionRuntimeModule } from 'azion/types';

import { BuildOptions as ESBuildConfig, type Plugin as EsbuildPlugin } from 'esbuild';
import { Configuration as WebpackConfig, type WebpackPluginInstance as WebpackPlugin } from 'webpack';
Expand Down Expand Up @@ -122,7 +122,7 @@ export type AzionRuleCriteria = AzionRuleCriteriaWithValue | AzionRuleCriteriaWi
/**
* Request rule configuration for Azion.
*/
export type AzionRequestRule = {
export type AzionRuntimeRequestRule = {
/** Rule name */
name: string;
/** Rule description */
Expand Down Expand Up @@ -253,7 +253,7 @@ export type AzionResponseRule = {
*/
export type AzionRules = {
/** Request rules */
request?: AzionRequestRule[];
request?: AzionRuntimeRequestRule[];
/** Response rules */
response?: AzionResponseRule[];
};
Expand All @@ -278,7 +278,6 @@ export interface AzionBuild<T extends WebpackConfig | ESBuildConfig | unknown =
bundler?: 'webpack' | 'esbuild';
preset?: PresetInput;
polyfills?: boolean;
worker?: boolean;
extend?: (context: T) => T;
memoryFS?: {
injectionDirs: string[];
Expand Down Expand Up @@ -567,7 +566,7 @@ export type PresetMetadata = {

export interface AzionBuildPreset {
config: AzionConfig;
handler?: (event: FetchEvent) => Promise<Response>;
handler?: AzionRuntimeModule;
prebuild?: (config: BuildConfiguration, ctx: BuildContext) => Promise<void | AzionPrebuildResult>;
postbuild?: (config: BuildConfiguration, ctx: BuildContext) => Promise<void>;
metadata: PresetMetadata;
Expand Down
45 changes: 31 additions & 14 deletions packages/presets/src/presets/angular/handler.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import { FetchEvent } from 'azion/types';
import { AzionRuntimeModule, AzionRuntimeRequest } from 'azion/types';
import { mountSPA } from 'azion/utils/edge';
import metadata from './metadata';

/**
* Handles the 'fetch' event.
* @param {import('azion/types').FetchEvent} event - The fetch event.
* @returns {Promise<Response>} The response for the request.
*/
async function handler(event: FetchEvent): Promise<Response> {
try {
const myApp = await mountSPA(event.request.url);
return myApp;
} catch (e) {
return new Response('Not Found', { status: 500 });
}
}
const handler: AzionRuntimeModule = {
/**
* Handles the 'fetch' event using Azion Workers pattern.
* @param {Request} request - The request object with metadata.
* @param {Object} env - The environment context containing Azion services.
* @param {Object} ctx - The execution context.
* @returns {Promise<Response>} The response for the request.
*/
fetch: async (request: AzionRuntimeRequest): Promise<Response> => {
try {
const myApp: Response = await mountSPA(request.url);
return myApp;
} catch (e) {
return new Response(
JSON.stringify({
error: `Failed to mount ${metadata.name} application`,
message: e instanceof Error ? e.message : 'Unknown error occurred',
path: request.url,
}),
{
status: 500,
headers: {
'Content-Type': 'application/json',
},
},
);
}
},
};

export default handler;
45 changes: 31 additions & 14 deletions packages/presets/src/presets/astro/handler.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import { FetchEvent } from 'azion/types';
import { AzionRuntimeModule, AzionRuntimeRequest } from 'azion/types';
import { mountMPA } from 'azion/utils/edge';
import metadata from './metadata';

/**
* Handles the 'fetch' event.
* @param {import('azion/types').FetchEvent} event - The fetch event.
* @returns {Promise<Response>} The response for the request.
*/
async function handler(event: FetchEvent): Promise<Response> {
try {
const myApp = await mountMPA(event.request.url);
return myApp;
} catch (e) {
return new Response('Not Found', { status: 500 });
}
}
const handler: AzionRuntimeModule = {
/**
* Handles the 'fetch' event using Azion Workers pattern.
* @param {Request} request - The request object with metadata.
* @param {Object} env - The environment context containing Azion services.
* @param {Object} ctx - The execution context.
* @returns {Promise<Response>} The response for the request.
*/
fetch: async (request: AzionRuntimeRequest): Promise<Response> => {
try {
const myApp: Response = await mountMPA(request.url);
return myApp;
} catch (e) {
return new Response(
JSON.stringify({
error: `Failed to mount ${metadata.name} application`,
message: e instanceof Error ? e.message : 'Unknown error occurred',
path: request.url,
}),
{
status: 500,
headers: {
'Content-Type': 'application/json',
},
},
);
}
},
};

export default handler;
45 changes: 31 additions & 14 deletions packages/presets/src/presets/docusaurus/handler.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import { FetchEvent } from 'azion/types';
import { AzionRuntimeModule, AzionRuntimeRequest } from 'azion/types';
import { mountMPA } from 'azion/utils/edge';
import metadata from './metadata';

/**
* Handles the 'fetch' event.
* @param {import('azion/types').FetchEvent} event - The fetch event.
* @returns {Promise<Response>} The response for the request.
*/
async function handler(event: FetchEvent): Promise<Response> {
try {
const myApp = await mountMPA(event.request.url);
return myApp;
} catch (e) {
return new Response('Not Found', { status: 500 });
}
}
const handler: AzionRuntimeModule = {
/**
* Handles the 'fetch' event using Azion Workers pattern.
* @param {Request} request - The request object with metadata.
* @param {Object} env - The environment context containing Azion services.
* @param {Object} ctx - The execution context.
* @returns {Promise<Response>} The response for the request.
*/
fetch: async (request: AzionRuntimeRequest): Promise<Response> => {
try {
const myApp: Response = await mountMPA(request.url);
return myApp;
} catch (e) {
return new Response(
JSON.stringify({
error: `Failed to mount ${metadata.name} application`,
message: e instanceof Error ? e.message : 'Unknown error occurred',
path: request.url,
}),
{
status: 500,
headers: {
'Content-Type': 'application/json',
},
},
);
}
},
};

export default handler;
45 changes: 31 additions & 14 deletions packages/presets/src/presets/eleventy/handler.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import { FetchEvent } from 'azion/types';
import { AzionRuntimeModule, AzionRuntimeRequest } from 'azion/types';
import { mountMPA } from 'azion/utils/edge';
import metadata from './metadata';

/**
* Handles the 'fetch' event.
* @param {import('azion/types').FetchEvent} event - The fetch event.
* @returns {Promise<Response>} The response for the request.
*/
async function handler(event: FetchEvent): Promise<Response> {
try {
const myApp = await mountMPA(event.request.url);
return myApp;
} catch (e) {
return new Response('Not Found', { status: 500 });
}
}
const handler: AzionRuntimeModule = {
/**
* Handles the 'fetch' event using Azion Workers pattern.
* @param {Request} request - The request object with metadata.
* @param {Object} env - The environment context containing Azion services.
* @param {Object} ctx - The execution context.
* @returns {Promise<Response>} The response for the request.
*/
fetch: async (request: AzionRuntimeRequest): Promise<Response> => {
try {
const myApp: Response = await mountMPA(request.url);
return myApp;
} catch (e) {
return new Response(
JSON.stringify({
error: `Failed to mount ${metadata.name} application`,
message: e instanceof Error ? e.message : 'Unknown error occurred',
path: request.url,
}),
{
status: 500,
headers: {
'Content-Type': 'application/json',
},
},
);
}
},
};

export default handler;
Loading