@uniformdev/canvas-sveltekit 20.48.1-alpha.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +2 -0
- package/README.md +3 -0
- package/dist/components/UniformRichText.svelte +49 -0
- package/dist/components/UniformRichText.svelte.d.ts +16 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +1 -0
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/index.js +1 -0
- package/dist/helpers/resolvePathFromParams.d.ts +19 -0
- package/dist/helpers/resolvePathFromParams.js +19 -0
- package/dist/hooks/createUniformHandle.d.ts +41 -0
- package/dist/hooks/createUniformHandle.js +43 -0
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.js +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +16 -0
- package/dist/isr/config.d.ts +49 -0
- package/dist/isr/config.js +30 -0
- package/dist/isr/createRevalidateHandler.d.ts +33 -0
- package/dist/isr/createRevalidateHandler.js +50 -0
- package/dist/isr/index.d.ts +2 -0
- package/dist/isr/index.js +2 -0
- package/dist/logging/index.d.ts +6 -0
- package/dist/logging/index.js +6 -0
- package/dist/logging/logCompositionIssues.d.ts +19 -0
- package/dist/logging/logCompositionIssues.js +49 -0
- package/dist/logging/logCompositionResponse.d.ts +5 -0
- package/dist/logging/logCompositionResponse.js +40 -0
- package/dist/logging/logCompositionRouteResponse.d.ts +5 -0
- package/dist/logging/logCompositionRouteResponse.js +20 -0
- package/dist/logging/logNotFoundRouteResponse.d.ts +5 -0
- package/dist/logging/logNotFoundRouteResponse.js +8 -0
- package/dist/logging/logRedirectRouteResponse.d.ts +5 -0
- package/dist/logging/logRedirectRouteResponse.js +8 -0
- package/dist/logging/logRouteResponse.d.ts +5 -0
- package/dist/logging/logRouteResponse.js +17 -0
- package/dist/models.d.ts +61 -0
- package/dist/models.js +4 -0
- package/dist/preview/createPreviewHandler.d.ts +29 -0
- package/dist/preview/createPreviewHandler.js +35 -0
- package/dist/preview/index.d.ts +4 -0
- package/dist/preview/index.js +4 -0
- package/dist/preview/previewCookie.d.ts +33 -0
- package/dist/preview/previewCookie.js +58 -0
- package/dist/preview/previewHandlerGet.d.ts +29 -0
- package/dist/preview/previewHandlerGet.js +138 -0
- package/dist/preview/previewHandlerPost.d.ts +29 -0
- package/dist/preview/previewHandlerPost.js +44 -0
- package/dist/route/createRouteFetcher.d.ts +20 -0
- package/dist/route/createRouteFetcher.js +90 -0
- package/dist/route/createUniformEntries.d.ts +26 -0
- package/dist/route/createUniformEntries.js +72 -0
- package/dist/route/createUniformLoad.d.ts +33 -0
- package/dist/route/createUniformLoad.js +71 -0
- package/dist/route/index.d.ts +4 -0
- package/dist/route/index.js +3 -0
- package/dist/route/types.d.ts +69 -0
- package/dist/route/types.js +1 -0
- package/package.json +75 -0
package/LICENSE.txt
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { ComponentInstance } from '@uniformdev/canvas';
|
|
3
|
+
import type { ParameterRichTextValue } from '@uniformdev/richtext';
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
/** The ID of the parameter containing rich text */
|
|
7
|
+
parameterId: string;
|
|
8
|
+
/** The component instance containing the parameter */
|
|
9
|
+
component: ComponentInstance;
|
|
10
|
+
/** Custom render function for the rich text */
|
|
11
|
+
render?: (value: string | undefined) => string;
|
|
12
|
+
/** HTML element to wrap the content */
|
|
13
|
+
as?: string;
|
|
14
|
+
/** Additional HTML attributes */
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let { parameterId, component, render, as: Tag = 'div', ...props }: Props = $props();
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Extracts HTML from rich text parameter value.
|
|
22
|
+
* Handles both simple string values and structured rich text objects.
|
|
23
|
+
*/
|
|
24
|
+
function getHtmlValue(value: ParameterRichTextValue | string | undefined): string | undefined {
|
|
25
|
+
if (!value) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (typeof value === 'string') {
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Handle structured rich text value (Lexical serialized state)
|
|
34
|
+
// For now, return undefined as rich text needs to be rendered
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Use $derived for reactive values
|
|
39
|
+
const parameter = $derived(component?.parameters?.[parameterId]);
|
|
40
|
+
const rawValue = $derived(parameter?.value as ParameterRichTextValue | string | undefined);
|
|
41
|
+
const htmlValue = $derived(getHtmlValue(rawValue));
|
|
42
|
+
const finalValue = $derived(render ? render(htmlValue) : htmlValue);
|
|
43
|
+
</script>
|
|
44
|
+
|
|
45
|
+
{#if finalValue}
|
|
46
|
+
<svelte:element this={Tag} {...props}>
|
|
47
|
+
{@html finalValue}
|
|
48
|
+
</svelte:element>
|
|
49
|
+
{/if}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ComponentInstance } from '@uniformdev/canvas';
|
|
2
|
+
interface Props {
|
|
3
|
+
/** The ID of the parameter containing rich text */
|
|
4
|
+
parameterId: string;
|
|
5
|
+
/** The component instance containing the parameter */
|
|
6
|
+
component: ComponentInstance;
|
|
7
|
+
/** Custom render function for the rich text */
|
|
8
|
+
render?: (value: string | undefined) => string;
|
|
9
|
+
/** HTML element to wrap the content */
|
|
10
|
+
as?: string;
|
|
11
|
+
/** Additional HTML attributes */
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
}
|
|
14
|
+
declare const UniformRichText: import("svelte").Component<Props, {}, "">;
|
|
15
|
+
type UniformRichText = ReturnType<typeof UniformRichText>;
|
|
16
|
+
export default UniformRichText;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as UniformRichText } from './UniformRichText.svelte';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as UniformRichText } from './UniformRichText.svelte';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { resolvePathFromParams } from './resolvePathFromParams.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { resolvePathFromParams } from './resolvePathFromParams.js';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves a URL path from SvelteKit route parameters.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* // For route /[...path]/+page.svelte with URL /blog/my-post
|
|
6
|
+
* resolvePathFromParams({ param: 'path', params: { path: 'blog/my-post' } })
|
|
7
|
+
* // Returns: '/blog/my-post'
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // For route /[slug]/+page.svelte with URL /about
|
|
11
|
+
* resolvePathFromParams({ param: 'slug', params: { slug: 'about' } })
|
|
12
|
+
* // Returns: '/about'
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolvePathFromParams({ param, params, }: {
|
|
15
|
+
/** The name of the route parameter (default: 'path') */
|
|
16
|
+
param?: string;
|
|
17
|
+
/** The params object from SvelteKit load event */
|
|
18
|
+
params: Record<string, string> | undefined;
|
|
19
|
+
}): string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves a URL path from SvelteKit route parameters.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* // For route /[...path]/+page.svelte with URL /blog/my-post
|
|
6
|
+
* resolvePathFromParams({ param: 'path', params: { path: 'blog/my-post' } })
|
|
7
|
+
* // Returns: '/blog/my-post'
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // For route /[slug]/+page.svelte with URL /about
|
|
11
|
+
* resolvePathFromParams({ param: 'slug', params: { slug: 'about' } })
|
|
12
|
+
* // Returns: '/about'
|
|
13
|
+
*/
|
|
14
|
+
export function resolvePathFromParams({ param = 'path', params, }) {
|
|
15
|
+
const pathValue = params?.[param] || '';
|
|
16
|
+
// SvelteKit rest params come as a single string with slashes
|
|
17
|
+
// e.g., /blog/my-post becomes { path: 'blog/my-post' }
|
|
18
|
+
return `/${pathValue}`;
|
|
19
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Handle, RequestEvent } from '@sveltejs/kit';
|
|
2
|
+
import type { UniformPreviewData } from '../models.js';
|
|
3
|
+
/**
|
|
4
|
+
* Options for creating the Uniform handle hook.
|
|
5
|
+
*/
|
|
6
|
+
export type CreateUniformHandleOptions = {
|
|
7
|
+
/**
|
|
8
|
+
* Optional callback when preview data is present.
|
|
9
|
+
* Useful for logging or additional setup.
|
|
10
|
+
*/
|
|
11
|
+
onPreview?: (event: RequestEvent, previewData: UniformPreviewData) => void | Promise<void>;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Creates a SvelteKit handle hook that integrates Uniform preview mode.
|
|
15
|
+
*
|
|
16
|
+
* This hook:
|
|
17
|
+
* - Makes preview data available via `event.locals.uniformPreview`
|
|
18
|
+
* - Optionally calls a callback when in preview mode
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // In src/hooks.server.ts
|
|
22
|
+
* import { createUniformHandle } from '@uniformdev/canvas-sveltekit';
|
|
23
|
+
* import { sequence } from '@sveltejs/kit/hooks';
|
|
24
|
+
*
|
|
25
|
+
* const uniformHandle = createUniformHandle({
|
|
26
|
+
* onPreview: (event, previewData) => {
|
|
27
|
+
* console.log('Preview mode active:', previewData);
|
|
28
|
+
* },
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* export const handle = sequence(uniformHandle);
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* // Access preview data in load functions
|
|
35
|
+
* export const load = async ({ locals }) => {
|
|
36
|
+
* if (locals.uniformPreview?.isUniformContextualEditing) {
|
|
37
|
+
* // In contextual editing mode
|
|
38
|
+
* }
|
|
39
|
+
* };
|
|
40
|
+
*/
|
|
41
|
+
export declare function createUniformHandle(options?: CreateUniformHandleOptions): Handle;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { getPreviewData } from '../preview/previewCookie.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a SvelteKit handle hook that integrates Uniform preview mode.
|
|
4
|
+
*
|
|
5
|
+
* This hook:
|
|
6
|
+
* - Makes preview data available via `event.locals.uniformPreview`
|
|
7
|
+
* - Optionally calls a callback when in preview mode
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // In src/hooks.server.ts
|
|
11
|
+
* import { createUniformHandle } from '@uniformdev/canvas-sveltekit';
|
|
12
|
+
* import { sequence } from '@sveltejs/kit/hooks';
|
|
13
|
+
*
|
|
14
|
+
* const uniformHandle = createUniformHandle({
|
|
15
|
+
* onPreview: (event, previewData) => {
|
|
16
|
+
* console.log('Preview mode active:', previewData);
|
|
17
|
+
* },
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* export const handle = sequence(uniformHandle);
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* // Access preview data in load functions
|
|
24
|
+
* export const load = async ({ locals }) => {
|
|
25
|
+
* if (locals.uniformPreview?.isUniformContextualEditing) {
|
|
26
|
+
* // In contextual editing mode
|
|
27
|
+
* }
|
|
28
|
+
* };
|
|
29
|
+
*/
|
|
30
|
+
export function createUniformHandle(options) {
|
|
31
|
+
const { onPreview } = options ?? {};
|
|
32
|
+
return async ({ event, resolve }) => {
|
|
33
|
+
// Parse preview data from cookie and attach to locals
|
|
34
|
+
const previewData = getPreviewData(event.cookies);
|
|
35
|
+
if (previewData) {
|
|
36
|
+
// Attach to locals for access in load functions
|
|
37
|
+
event.locals.uniformPreview = previewData;
|
|
38
|
+
// Call optional callback
|
|
39
|
+
await onPreview?.(event, previewData);
|
|
40
|
+
}
|
|
41
|
+
return resolve(event);
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createUniformHandle, type CreateUniformHandleOptions } from './createUniformHandle.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createUniformHandle } from './createUniformHandle.js';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { type ResolveFullPath, type ResolveFullPathOptions, type RouteCompositionResult, UNIFORM_PREVIEW_COOKIE_NAME, type UniformPageData, type UniformPreviewData, } from './models.js';
|
|
2
|
+
export { UniformRichText } from './components/index.js';
|
|
3
|
+
export { resolvePathFromParams } from './helpers/index.js';
|
|
4
|
+
export { type CompositionIssue, logCompositionIssues, logCompositionResponse, logCompositionRouteResponse, logNotFoundRouteResponse, logRedirectRouteResponse, logRouteResponse, } from './logging/index.js';
|
|
5
|
+
export { createUniformHandle, type CreateUniformHandleOptions } from './hooks/index.js';
|
|
6
|
+
export { createRevalidateHandler, type CreateRevalidateHandlerOptions, createVercelIsrConfig, defaultIsrConfig, type VercelIsrConfig, } from './isr/index.js';
|
|
7
|
+
export { createRouteFetcher, createUniformEntries, type CreateUniformEntriesOptions, createUniformLoad, type CreateUniformLoadOptions, type HandleRouteCompositionFunction, type HandleRouteNotFoundFunction, type HandleRouteRedirectFunction, type RouteHandlerOptions, type UniformEntry, type UniformLoadResult, } from './route/index.js';
|
|
8
|
+
export { clearPreviewData, createPreviewHandler, createPreviewHandlerGet, type CreatePreviewHandlerGetOptions, type CreatePreviewHandlerOptions, createPreviewHandlerPost, type CreatePreviewHandlerPostOptions, deserializePreviewData, getPreviewData, isPreviewMode, serializePreviewData, setPreviewData, } from './preview/index.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Models and types
|
|
2
|
+
export { UNIFORM_PREVIEW_COOKIE_NAME, } from './models.js';
|
|
3
|
+
// Components
|
|
4
|
+
export { UniformRichText } from './components/index.js';
|
|
5
|
+
// Helpers
|
|
6
|
+
export { resolvePathFromParams } from './helpers/index.js';
|
|
7
|
+
// Logging
|
|
8
|
+
export { logCompositionIssues, logCompositionResponse, logCompositionRouteResponse, logNotFoundRouteResponse, logRedirectRouteResponse, logRouteResponse, } from './logging/index.js';
|
|
9
|
+
// Hooks
|
|
10
|
+
export { createUniformHandle } from './hooks/index.js';
|
|
11
|
+
// ISR
|
|
12
|
+
export { createRevalidateHandler, createVercelIsrConfig, defaultIsrConfig, } from './isr/index.js';
|
|
13
|
+
// Route - also exported from ./route subpath
|
|
14
|
+
export { createRouteFetcher, createUniformEntries, createUniformLoad, } from './route/index.js';
|
|
15
|
+
// Preview - also exported from ./preview subpath
|
|
16
|
+
export { clearPreviewData, createPreviewHandler, createPreviewHandlerGet, createPreviewHandlerPost, deserializePreviewData, getPreviewData, isPreviewMode, serializePreviewData, setPreviewData, } from './preview/index.js';
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for Vercel ISR (Incremental Static Regeneration).
|
|
3
|
+
* These are passed to the page's `config` export.
|
|
4
|
+
*
|
|
5
|
+
* @see https://vercel.com/docs/frameworks/sveltekit#incremental-static-regeneration-isr
|
|
6
|
+
*/
|
|
7
|
+
export type VercelIsrConfig = {
|
|
8
|
+
/**
|
|
9
|
+
* Expiration time in seconds. After this time, the page will be regenerated
|
|
10
|
+
* on the next request.
|
|
11
|
+
*/
|
|
12
|
+
expiration: number | false;
|
|
13
|
+
/**
|
|
14
|
+
* A list of valid query string parameter names that can bust the ISR cache.
|
|
15
|
+
*/
|
|
16
|
+
allowQuery?: string[];
|
|
17
|
+
/**
|
|
18
|
+
* If true, the request won't wait for the fresh page to be generated.
|
|
19
|
+
*/
|
|
20
|
+
passQuery?: boolean;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Creates a Vercel ISR config object for use in +page.server.ts.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* // In +page.server.ts
|
|
27
|
+
* import { createVercelIsrConfig } from '@uniformdev/canvas-sveltekit';
|
|
28
|
+
*
|
|
29
|
+
* export const config = createVercelIsrConfig({
|
|
30
|
+
* expiration: 60, // Revalidate every 60 seconds
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* // With custom options
|
|
35
|
+
* export const config = createVercelIsrConfig({
|
|
36
|
+
* expiration: 300, // 5 minutes
|
|
37
|
+
* allowQuery: ['preview', 'draft'],
|
|
38
|
+
* });
|
|
39
|
+
*/
|
|
40
|
+
export declare function createVercelIsrConfig(options: VercelIsrConfig): {
|
|
41
|
+
isr: VercelIsrConfig;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Default ISR configuration for Uniform Canvas pages.
|
|
45
|
+
* Revalidates every 60 seconds.
|
|
46
|
+
*/
|
|
47
|
+
export declare const defaultIsrConfig: {
|
|
48
|
+
isr: VercelIsrConfig;
|
|
49
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a Vercel ISR config object for use in +page.server.ts.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* // In +page.server.ts
|
|
6
|
+
* import { createVercelIsrConfig } from '@uniformdev/canvas-sveltekit';
|
|
7
|
+
*
|
|
8
|
+
* export const config = createVercelIsrConfig({
|
|
9
|
+
* expiration: 60, // Revalidate every 60 seconds
|
|
10
|
+
* });
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // With custom options
|
|
14
|
+
* export const config = createVercelIsrConfig({
|
|
15
|
+
* expiration: 300, // 5 minutes
|
|
16
|
+
* allowQuery: ['preview', 'draft'],
|
|
17
|
+
* });
|
|
18
|
+
*/
|
|
19
|
+
export function createVercelIsrConfig(options) {
|
|
20
|
+
return {
|
|
21
|
+
isr: options,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Default ISR configuration for Uniform Canvas pages.
|
|
26
|
+
* Revalidates every 60 seconds.
|
|
27
|
+
*/
|
|
28
|
+
export const defaultIsrConfig = createVercelIsrConfig({
|
|
29
|
+
expiration: 60,
|
|
30
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type RequestHandler } from '@sveltejs/kit';
|
|
2
|
+
/**
|
|
3
|
+
* Options for creating the revalidation handler.
|
|
4
|
+
*/
|
|
5
|
+
export type CreateRevalidateHandlerOptions = {
|
|
6
|
+
/** Function that returns the revalidation secret. If set, requests must include matching secret. */
|
|
7
|
+
secret?: () => string;
|
|
8
|
+
/**
|
|
9
|
+
* Function to revalidate paths on Vercel.
|
|
10
|
+
* By default, this is a no-op as Vercel handles ISR automatically.
|
|
11
|
+
* You can provide a custom implementation to integrate with Vercel's revalidation API.
|
|
12
|
+
*/
|
|
13
|
+
revalidate?: (paths: string[]) => Promise<void>;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Creates a SvelteKit request handler for on-demand ISR revalidation.
|
|
17
|
+
*
|
|
18
|
+
* Note: Vercel ISR is typically handled automatically via the `config.isr` export.
|
|
19
|
+
* This handler is useful for webhooks that need to trigger revalidation programmatically.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // In src/routes/api/revalidate/+server.ts
|
|
23
|
+
* import { createRevalidateHandler } from '@uniformdev/canvas-sveltekit';
|
|
24
|
+
*
|
|
25
|
+
* export const POST = createRevalidateHandler({
|
|
26
|
+
* secret: () => process.env.REVALIDATION_SECRET,
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* // Then call from webhook:
|
|
30
|
+
* // POST /api/revalidate?secret=xxx
|
|
31
|
+
* // Body: { paths: ['/blog/my-post', '/about'] }
|
|
32
|
+
*/
|
|
33
|
+
export declare function createRevalidateHandler(options?: CreateRevalidateHandlerOptions): RequestHandler;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { error, json } from '@sveltejs/kit';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a SvelteKit request handler for on-demand ISR revalidation.
|
|
4
|
+
*
|
|
5
|
+
* Note: Vercel ISR is typically handled automatically via the `config.isr` export.
|
|
6
|
+
* This handler is useful for webhooks that need to trigger revalidation programmatically.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* // In src/routes/api/revalidate/+server.ts
|
|
10
|
+
* import { createRevalidateHandler } from '@uniformdev/canvas-sveltekit';
|
|
11
|
+
*
|
|
12
|
+
* export const POST = createRevalidateHandler({
|
|
13
|
+
* secret: () => process.env.REVALIDATION_SECRET,
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* // Then call from webhook:
|
|
17
|
+
* // POST /api/revalidate?secret=xxx
|
|
18
|
+
* // Body: { paths: ['/blog/my-post', '/about'] }
|
|
19
|
+
*/
|
|
20
|
+
export function createRevalidateHandler(options) {
|
|
21
|
+
const { secret, revalidate } = options ?? {};
|
|
22
|
+
return async ({ request, url }) => {
|
|
23
|
+
// Validate secret if configured
|
|
24
|
+
const revalidateSecret = typeof secret === 'function' ? secret() : secret;
|
|
25
|
+
if (revalidateSecret) {
|
|
26
|
+
const providedSecret = url.searchParams.get('secret');
|
|
27
|
+
if (providedSecret !== revalidateSecret) {
|
|
28
|
+
error(401, 'Invalid secret');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
// Parse paths from request body
|
|
32
|
+
const body = (await request.json());
|
|
33
|
+
const paths = body?.paths;
|
|
34
|
+
if (!paths || !Array.isArray(paths) || paths.length === 0) {
|
|
35
|
+
error(400, 'Missing or invalid "paths" parameter');
|
|
36
|
+
}
|
|
37
|
+
// Call custom revalidation function if provided
|
|
38
|
+
if (revalidate) {
|
|
39
|
+
await revalidate(paths);
|
|
40
|
+
}
|
|
41
|
+
// Note: On Vercel, actual ISR revalidation happens automatically
|
|
42
|
+
// This endpoint is primarily for logging/tracking purposes
|
|
43
|
+
// or for custom cache invalidation logic
|
|
44
|
+
return json({
|
|
45
|
+
revalidated: true,
|
|
46
|
+
paths,
|
|
47
|
+
timestamp: Date.now(),
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { type CompositionIssue, logCompositionIssues } from './logCompositionIssues.js';
|
|
2
|
+
export { logCompositionResponse } from './logCompositionResponse.js';
|
|
3
|
+
export { logCompositionRouteResponse } from './logCompositionRouteResponse.js';
|
|
4
|
+
export { logNotFoundRouteResponse } from './logNotFoundRouteResponse.js';
|
|
5
|
+
export { logRedirectRouteResponse } from './logRedirectRouteResponse.js';
|
|
6
|
+
export { logRouteResponse } from './logRouteResponse.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { logCompositionIssues } from './logCompositionIssues.js';
|
|
2
|
+
export { logCompositionResponse } from './logCompositionResponse.js';
|
|
3
|
+
export { logCompositionRouteResponse } from './logCompositionRouteResponse.js';
|
|
4
|
+
export { logNotFoundRouteResponse } from './logNotFoundRouteResponse.js';
|
|
5
|
+
export { logRedirectRouteResponse } from './logRedirectRouteResponse.js';
|
|
6
|
+
export { logRouteResponse } from './logRouteResponse.js';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Issue from composition resolution.
|
|
3
|
+
*/
|
|
4
|
+
export type CompositionIssue = {
|
|
5
|
+
type: string;
|
|
6
|
+
message: string;
|
|
7
|
+
componentPath?: string;
|
|
8
|
+
componentType?: string;
|
|
9
|
+
expression?: {
|
|
10
|
+
pointer?: string;
|
|
11
|
+
};
|
|
12
|
+
dataName?: string;
|
|
13
|
+
dataType?: string;
|
|
14
|
+
inputName?: string;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Writes colorized logs about composition fetch warnings or errors to the console.
|
|
18
|
+
*/
|
|
19
|
+
export declare function logCompositionIssues(prefix: string, issues: CompositionIssue[], colour?: 'red' | 'green' | 'yellow' | 'white'): void;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { gray, green, italic, red, white, yellow } from 'colorette';
|
|
2
|
+
/**
|
|
3
|
+
* Writes colorized logs about composition fetch warnings or errors to the console.
|
|
4
|
+
*/
|
|
5
|
+
export function logCompositionIssues(prefix, issues, colour = 'red') {
|
|
6
|
+
const colours = { red, green, yellow, white };
|
|
7
|
+
// eslint-disable-next-line no-console
|
|
8
|
+
console.error(`${colours[colour](prefix)}\n${issues
|
|
9
|
+
.map((issue) => `${colours[colour]('>')} ${italic(gray(indent(issue.type.toUpperCase(), 7)))} ${renderIssue(issue)}`)
|
|
10
|
+
.join('\n')}`);
|
|
11
|
+
}
|
|
12
|
+
function renderIssue(issue) {
|
|
13
|
+
let path;
|
|
14
|
+
let type;
|
|
15
|
+
const message = issue.message;
|
|
16
|
+
let detail;
|
|
17
|
+
if (issue.type !== 'config') {
|
|
18
|
+
path = issue.componentPath;
|
|
19
|
+
type = issue.componentType;
|
|
20
|
+
}
|
|
21
|
+
if (issue.type === 'binding') {
|
|
22
|
+
detail = `Binding expression: ${issue.expression?.pointer}`;
|
|
23
|
+
}
|
|
24
|
+
else if (issue.type === 'data') {
|
|
25
|
+
detail = `Data resource name: ${issue.dataName}, data type: ${issue.dataType}`;
|
|
26
|
+
}
|
|
27
|
+
if (issue.type === 'input') {
|
|
28
|
+
detail = issue.inputName;
|
|
29
|
+
}
|
|
30
|
+
let result = `${white(blockify(message))}`;
|
|
31
|
+
if (detail) {
|
|
32
|
+
result += `\n${indent(' ', 10)}${gray(`${detail}`)}`;
|
|
33
|
+
}
|
|
34
|
+
if (path && type) {
|
|
35
|
+
result += `\n${indent(' ', 10)}${gray(`Occurred on component ${type} at slot path ${path}`)}`;
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
function indent(str, len) {
|
|
40
|
+
const indentSize = len ?? 10;
|
|
41
|
+
return str.padStart(indentSize);
|
|
42
|
+
}
|
|
43
|
+
function blockify(str, len) {
|
|
44
|
+
const indentSize = len ?? 10;
|
|
45
|
+
return str
|
|
46
|
+
.split('\n')
|
|
47
|
+
.map((line, index) => (index === 0 ? line : `${indent(' ', indentSize)}${line}`))
|
|
48
|
+
.join('\n');
|
|
49
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { CompositionResolvedGetResponse } from '@uniformdev/canvas';
|
|
2
|
+
/**
|
|
3
|
+
* Writes colorized logs about a composition API fetch to the console.
|
|
4
|
+
*/
|
|
5
|
+
export declare function logCompositionResponse(compositionApiResponse: CompositionResolvedGetResponse, duration?: number): void;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { white } from 'colorette';
|
|
2
|
+
import { logCompositionIssues } from './logCompositionIssues.js';
|
|
3
|
+
/**
|
|
4
|
+
* Writes colorized logs about a composition API fetch to the console.
|
|
5
|
+
*/
|
|
6
|
+
export function logCompositionResponse(compositionApiResponse, duration) {
|
|
7
|
+
const resErrors = compositionApiResponse.errors ?? [];
|
|
8
|
+
const resWarnings = compositionApiResponse.warnings ?? [];
|
|
9
|
+
if (resErrors.length > 0) {
|
|
10
|
+
logCompositionIssues(`Composition data error${resErrors.length === 1 ? '' : 's'}; some content may be missing`, resErrors, 'red');
|
|
11
|
+
}
|
|
12
|
+
if (resWarnings.length > 0) {
|
|
13
|
+
logCompositionIssues(`Composition data warning${resWarnings.length === 1 ? '' : 's'}; some content may be missing`, resWarnings, 'yellow');
|
|
14
|
+
}
|
|
15
|
+
if (compositionApiResponse.diagnostics) {
|
|
16
|
+
// eslint-disable-next-line no-console
|
|
17
|
+
console.log(`\nDiagnostics`);
|
|
18
|
+
const diagnostics = compositionApiResponse.diagnostics;
|
|
19
|
+
const table = {
|
|
20
|
+
[`${white('Composition')}`]: {
|
|
21
|
+
'Duration (ms)': diagnostics.originFetch?.duration ?? 'no data',
|
|
22
|
+
'Edge Cached': diagnostics.originFetch?.cacheHit,
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
if (diagnostics.data) {
|
|
26
|
+
diagnostics.data.forEach((d) => {
|
|
27
|
+
table[d.dataName] = {
|
|
28
|
+
'Duration (ms)': d.performance.duration ?? 'no data',
|
|
29
|
+
'Edge Cached': d.performance.cacheHit,
|
|
30
|
+
'Retry Count': d.performance.retryCount,
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
table['Total Response'] = {
|
|
35
|
+
'Duration (ms)': duration ?? 'no data',
|
|
36
|
+
};
|
|
37
|
+
// eslint-disable-next-line no-console
|
|
38
|
+
console.table(table);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { RouteGetResponseEdgehancedComposition } from '@uniformdev/canvas';
|
|
2
|
+
/**
|
|
3
|
+
* Writes colorized logs about a composition result from the route API to the console.
|
|
4
|
+
*/
|
|
5
|
+
export declare function logCompositionRouteResponse(matched: RouteGetResponseEdgehancedComposition, duration?: number): void;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { gray, green, red, yellow } from 'colorette';
|
|
2
|
+
import { logCompositionResponse } from './logCompositionResponse.js';
|
|
3
|
+
/**
|
|
4
|
+
* Writes colorized logs about a composition result from the route API to the console.
|
|
5
|
+
*/
|
|
6
|
+
export function logCompositionRouteResponse(matched, duration) {
|
|
7
|
+
const resErrors = matched.compositionApiResponse.errors ?? [];
|
|
8
|
+
const resWarnings = matched.compositionApiResponse.warnings ?? [];
|
|
9
|
+
const colour = resErrors.length > 0 ? red : resWarnings.length > 0 ? yellow : green;
|
|
10
|
+
// eslint-disable-next-line no-console
|
|
11
|
+
console.log(`${colour('Matched')} ${matched.type} ${matched.matchedRoute} (${duration}ms)`);
|
|
12
|
+
if (matched.dynamicInputs) {
|
|
13
|
+
const entries = Object.entries(matched.dynamicInputs);
|
|
14
|
+
if (entries.length > 0) {
|
|
15
|
+
// eslint-disable-next-line no-console
|
|
16
|
+
console.log(` ${gray('Matched dynamic inputs:\n ')}`, entries.map(([k, v]) => `${k}: ${v}`).join('\n '));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
logCompositionResponse(matched.compositionApiResponse, duration);
|
|
20
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { RouteGetResponseNotFound } from '@uniformdev/canvas';
|
|
2
|
+
/**
|
|
3
|
+
* Writes colorized logs about a not found result from the route API to the console.
|
|
4
|
+
*/
|
|
5
|
+
export declare function logNotFoundRouteResponse(_matched: RouteGetResponseNotFound, duration?: number): void;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { yellow } from 'colorette';
|
|
2
|
+
/**
|
|
3
|
+
* Writes colorized logs about a not found result from the route API to the console.
|
|
4
|
+
*/
|
|
5
|
+
export function logNotFoundRouteResponse(_matched, duration) {
|
|
6
|
+
// eslint-disable-next-line no-console
|
|
7
|
+
console.log(`${yellow('Not found')} no project map or redirect path match (${duration}ms)`);
|
|
8
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { RouteGetResponseRedirect } from '@uniformdev/canvas';
|
|
2
|
+
/**
|
|
3
|
+
* Writes colorized logs about a redirect result from the route API to the console.
|
|
4
|
+
*/
|
|
5
|
+
export declare function logRedirectRouteResponse(matched: RouteGetResponseRedirect, duration?: number): void;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { green } from 'colorette';
|
|
2
|
+
/**
|
|
3
|
+
* Writes colorized logs about a redirect result from the route API to the console.
|
|
4
|
+
*/
|
|
5
|
+
export function logRedirectRouteResponse(matched, duration) {
|
|
6
|
+
// eslint-disable-next-line no-console
|
|
7
|
+
console.log(`${green('Matched')} ${matched.type} ${matched.matchedRoute} (${duration}ms)\nTarget: ${matched.redirect.targetUrl}`);
|
|
8
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { logCompositionRouteResponse } from './logCompositionRouteResponse.js';
|
|
2
|
+
import { logNotFoundRouteResponse } from './logNotFoundRouteResponse.js';
|
|
3
|
+
import { logRedirectRouteResponse } from './logRedirectRouteResponse.js';
|
|
4
|
+
/**
|
|
5
|
+
* Writes colorized logs about a route API response to the console.
|
|
6
|
+
*/
|
|
7
|
+
export function logRouteResponse(response, duration) {
|
|
8
|
+
if (response.type === 'redirect') {
|
|
9
|
+
logRedirectRouteResponse(response, duration);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
if (response.type === 'notFound') {
|
|
13
|
+
logNotFoundRouteResponse(response, duration);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
logCompositionRouteResponse(response, duration);
|
|
17
|
+
}
|