@nordcraft/core 1.0.74 → 1.0.76
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/README.md +5 -1
- package/dist/api/LegacyToddleApi.d.ts +17 -16
- package/dist/api/LegacyToddleApi.js.map +1 -1
- package/dist/api/ToddleApiV2.d.ts +46 -45
- package/dist/api/ToddleApiV2.js.map +1 -1
- package/dist/api/api.d.ts +16 -15
- package/dist/api/api.js +3 -3
- package/dist/api/api.js.map +1 -1
- package/dist/api/apiTypes.d.ts +68 -68
- package/dist/api/headers.d.ts +6 -5
- package/dist/api/headers.js.map +1 -1
- package/dist/component/ToddleComponent.d.ts +11 -11
- package/dist/component/ToddleComponent.js +2 -2
- package/dist/component/ToddleComponent.js.map +1 -1
- package/dist/component/actionUtils.d.ts +2 -2
- package/dist/component/actionUtils.js +1 -0
- package/dist/component/actionUtils.js.map +1 -1
- package/dist/component/component.types.d.ts +140 -140
- package/dist/component/component.types.js.map +1 -1
- package/dist/component/schemas/zod-schemas.d.ts +4 -0
- package/dist/component/schemas/zod-schemas.js +1106 -0
- package/dist/component/schemas/zod-schemas.js.map +1 -0
- package/dist/formula/formula.d.ts +14 -14
- package/dist/formula/formula.js +1 -1
- package/dist/formula/formula.js.map +1 -1
- package/dist/formula/formulaTypes.d.ts +10 -9
- package/dist/formula/formulaUtils.d.ts +11 -10
- package/dist/formula/formulaUtils.js +8 -2
- package/dist/formula/formulaUtils.js.map +1 -1
- package/dist/styling/customProperty.d.ts +2 -1
- package/dist/styling/customProperty.js.map +1 -1
- package/dist/styling/style.css.js +2 -2
- package/dist/styling/style.css.js.map +1 -1
- package/dist/styling/theme.d.ts +3 -2
- package/dist/styling/theme.js.map +1 -1
- package/dist/styling/variantSelector.d.ts +36 -35
- package/dist/styling/variantSelector.js.map +1 -1
- package/dist/types.d.ts +16 -15
- package/dist/utils/collections.d.ts +2 -1
- package/dist/utils/collections.js.map +1 -1
- package/dist/utils/getNodeSelector.d.ts +4 -3
- package/dist/utils/getNodeSelector.js.map +1 -1
- package/package.json +4 -1
- package/src/api/LegacyToddleApi.ts +2 -1
- package/src/api/ToddleApiV2.ts +2 -1
- package/src/api/api.ts +10 -9
- package/src/api/apiTypes.ts +73 -70
- package/src/api/headers.ts +7 -5
- package/src/component/ToddleComponent.ts +2 -2
- package/src/component/actionUtils.ts +3 -2
- package/src/component/component.types.ts +136 -131
- package/src/component/schemas/zod-schemas.ts +1535 -0
- package/src/formula/formula.ts +21 -18
- package/src/formula/formulaTypes.ts +12 -9
- package/src/formula/formulaUtils.ts +20 -13
- package/src/styling/customProperty.ts +2 -1
- package/src/styling/style.css.ts +6 -3
- package/src/styling/theme.ts +3 -2
- package/src/styling/variantSelector.ts +36 -35
- package/src/types.ts +27 -15
- package/src/utils/collections.ts +2 -1
- package/src/utils/getNodeSelector.ts +4 -3
package/dist/types.d.ts
CHANGED
|
@@ -6,14 +6,14 @@ export type FormulaHandlerV2<R = unknown> = (args: Record<string, unknown>, ctx:
|
|
|
6
6
|
env: ToddleEnv;
|
|
7
7
|
}) => R | null;
|
|
8
8
|
export type ActionHandlerV2 = (args: Record<string, unknown>, ctx: {
|
|
9
|
-
triggerActionEvent: (trigger: string, data: any, event?: Event) => void;
|
|
9
|
+
triggerActionEvent: (trigger: string, data: any, event?: Nullable<Event>) => void;
|
|
10
10
|
root: Document | ShadowRoot;
|
|
11
|
-
}, event?: Event) => void | (() => void) | Promise<void> | Promise<() => void>;
|
|
11
|
+
}, event?: Nullable<Event>) => void | (() => void) | Promise<void> | Promise<() => void>;
|
|
12
12
|
export type ActionHandler<Args = unknown[]> = (args: Args, ctx: {
|
|
13
|
-
triggerActionEvent: (trigger: string, data: any, event?: Event) => void;
|
|
13
|
+
triggerActionEvent: (trigger: string, data: any, event?: Nullable<Event>) => void;
|
|
14
14
|
env: ToddleEnv;
|
|
15
15
|
abortSignal: AbortSignal;
|
|
16
|
-
}, event?: Event) => void;
|
|
16
|
+
}, event?: Nullable<Event>) => void;
|
|
17
17
|
export type FormulaHandler<T = void> = (args: unknown[], ctx: {
|
|
18
18
|
component: Component;
|
|
19
19
|
data: ComponentData;
|
|
@@ -22,20 +22,20 @@ export type FormulaHandler<T = void> = (args: unknown[], ctx: {
|
|
|
22
22
|
}) => T | null;
|
|
23
23
|
interface PluginActionBase {
|
|
24
24
|
name: string;
|
|
25
|
-
description?: string
|
|
26
|
-
arguments?: Array<{
|
|
25
|
+
description?: Nullable<string>;
|
|
26
|
+
arguments?: Nullable<Array<{
|
|
27
27
|
name: string;
|
|
28
28
|
formula: Formula;
|
|
29
|
-
}
|
|
30
|
-
events?: Record<string, {
|
|
29
|
+
}>>;
|
|
30
|
+
events?: Nullable<Record<string, {
|
|
31
31
|
dummyEvent?: any;
|
|
32
|
-
}
|
|
33
|
-
variableArguments: boolean
|
|
32
|
+
}>>;
|
|
33
|
+
variableArguments: Nullable<boolean>;
|
|
34
34
|
}
|
|
35
35
|
export interface PluginActionV2 extends PluginActionBase {
|
|
36
36
|
handler: ActionHandlerV2;
|
|
37
37
|
version: 2;
|
|
38
|
-
exported?: boolean
|
|
38
|
+
exported?: Nullable<boolean>;
|
|
39
39
|
}
|
|
40
40
|
export interface LegacyPluginAction extends PluginActionBase {
|
|
41
41
|
handler: string;
|
|
@@ -92,15 +92,16 @@ export interface Comment {
|
|
|
92
92
|
text: string;
|
|
93
93
|
}
|
|
94
94
|
export interface NordcraftMetadata {
|
|
95
|
-
'@nordcraft/metadata'?: {
|
|
96
|
-
comments: Record<string, Comment & {
|
|
95
|
+
'@nordcraft/metadata'?: Nullable<{
|
|
96
|
+
comments: Nullable<Partial<Record<string, Comment & {
|
|
97
97
|
index: number;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
98
|
+
}>>>;
|
|
99
|
+
}>;
|
|
100
100
|
}
|
|
101
101
|
export type RequireFields<T, K extends keyof T> = Omit<T, K> & {
|
|
102
102
|
[P in K]-?: NonNullable<T[P]>;
|
|
103
103
|
};
|
|
104
|
+
export type Nullable<T> = T | null | undefined;
|
|
104
105
|
export type NestedOmit<Schema, Path extends string> = Path extends `${infer Head}.${infer Tail}` ? Head extends keyof Schema ? {
|
|
105
106
|
[K in keyof Schema]: K extends Head ? NestedOmit<Schema[K], Tail> : Schema[K];
|
|
106
107
|
} : Schema : Omit<Schema, Path>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Nullable } from '../types';
|
|
1
2
|
export declare const isObject: (input: any) => input is Record<string, any>;
|
|
2
3
|
export declare const mapObject: <T, T2>(object: Record<string, T>, f: (kv: [string, T]) => [string, T2]) => Record<string, T2>;
|
|
3
4
|
export declare const mapValues: <T, T2>(object: Record<string, T>, f: (value: T) => T2) => Record<string, T2>;
|
|
@@ -18,4 +19,4 @@ export declare function get<T = any>(collection: T, [head, ...rest]: string[]):
|
|
|
18
19
|
export declare const set: <T = unknown>(collection: T, key: (string | number)[], value: any) => T;
|
|
19
20
|
export declare const sortObjectEntries: <T>(object: Record<string, T>, f: (kv: [string, T]) => string | number | boolean, ascending?: boolean) => [string, T][];
|
|
20
21
|
export declare const easySort: <T>(collection: T[], f: (item: T) => string | number | boolean, ascending?: boolean) => T[];
|
|
21
|
-
export declare const deepSortObject: (obj: any) => any[] | Record<string, any
|
|
22
|
+
export declare const deepSortObject: (obj: any) => Nullable<any[] | Record<string, any>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"collections.js","sourceRoot":"","sources":["../../src/utils/collections.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"collections.js","sourceRoot":"","sources":["../../src/utils/collections.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAElC,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,KAAU,EAAgC,EAAE,CACnE,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,CAAA;AAE7C,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,MAAyB,EACzB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2
|
+
import type { Nullable } from '../types';
|
|
2
3
|
type NodeSelectorOptions = {
|
|
3
4
|
componentName: string;
|
|
4
|
-
nodeId: string
|
|
5
|
-
variant?: StyleVariant
|
|
5
|
+
nodeId: Nullable<string>;
|
|
6
|
+
variant?: Nullable<StyleVariant>;
|
|
6
7
|
} | {
|
|
7
8
|
componentName?: never;
|
|
8
9
|
nodeId?: never;
|
|
9
|
-
variant?: StyleVariant
|
|
10
|
+
variant?: Nullable<StyleVariant>;
|
|
10
11
|
};
|
|
11
12
|
export declare function getNodeSelector(path: string, { componentName, nodeId, variant }?: NodeSelectorOptions): string;
|
|
12
13
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getNodeSelector.js","sourceRoot":"","sources":["../../src/utils/getNodeSelector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAqB,MAAM,4BAA4B,CAAA;
|
|
1
|
+
{"version":3,"file":"getNodeSelector.js","sourceRoot":"","sources":["../../src/utils/getNodeSelector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAqB,MAAM,4BAA4B,CAAA;AAe/E,MAAM,UAAU,eAAe,CAC7B,IAAY,EACZ,EAAE,aAAa,EAAE,MAAM,EAAE,OAAO,EAAE,GAAwB,EAAE,EACpD;IACR,IAAI,QAAQ,GAAG,aAAa,IAAI,IAAI,CAAA;IACpC,IAAI,aAAa,EAAE,CAAC;QAClB,QAAQ,IAAI,IAAI,aAAa,EAAE,CAAA;IACjC,CAAC;IACD,IAAI,MAAM,EAAE,CAAC;QACX,QAAQ,IAAI,MAAM,MAAM,EAAE,CAAA;IAC5B,CAAC;IACD,IAAI,OAAO,EAAE,CAAC;QACZ,QAAQ,IAAI,eAAe,CAAC,OAAO,CAAC,CAAA;IACtC,CAAC;IAED,OAAO,QAAQ,CAAA;AAAA,CAChB"}
|
package/package.json
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
getFormulasInAction,
|
|
5
5
|
getFormulasInFormula,
|
|
6
6
|
} from '../formula/formulaUtils'
|
|
7
|
+
import type { Nullable } from '../types'
|
|
7
8
|
import { isDefined } from '../utils/util'
|
|
8
9
|
import { type LegacyComponentAPI } from './apiTypes'
|
|
9
10
|
|
|
@@ -29,7 +30,7 @@ export class LegacyToddleApi<Handler> {
|
|
|
29
30
|
return this._apiReferences
|
|
30
31
|
}
|
|
31
32
|
const apis = new Set<string>()
|
|
32
|
-
const visitFormulaReference = (formula?: Formula
|
|
33
|
+
const visitFormulaReference = (formula?: Nullable<Formula>) => {
|
|
33
34
|
if (!isDefined(formula)) {
|
|
34
35
|
return
|
|
35
36
|
}
|
package/src/api/ToddleApiV2.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
getFormulasInAction,
|
|
7
7
|
getFormulasInFormula,
|
|
8
8
|
} from '../formula/formulaUtils'
|
|
9
|
+
import type { Nullable } from '../types'
|
|
9
10
|
import { isDefined } from '../utils/util'
|
|
10
11
|
import { HttpMethodsWithAllowedBody } from './api'
|
|
11
12
|
import { type ApiRequest } from './apiTypes'
|
|
@@ -32,7 +33,7 @@ export class ToddleApiV2<Handler> implements ApiRequest {
|
|
|
32
33
|
return this._apiReferences
|
|
33
34
|
}
|
|
34
35
|
const apis = new Set<string>()
|
|
35
|
-
const visitFormulaReference = (formula?: Formula
|
|
36
|
+
const visitFormulaReference = (formula?: Nullable<Formula>) => {
|
|
36
37
|
if (!isDefined(formula)) {
|
|
37
38
|
return
|
|
38
39
|
}
|
package/src/api/api.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Formula, FormulaContext } from '../formula/formula'
|
|
2
2
|
import { applyFormula } from '../formula/formula'
|
|
3
|
+
import type { Nullable } from '../types'
|
|
3
4
|
import { omitKeys, sortObjectEntries } from '../utils/collections'
|
|
4
5
|
import { hash } from '../utils/hash'
|
|
5
6
|
import { isDefined, isObject, toBoolean } from '../utils/util'
|
|
@@ -31,7 +32,7 @@ export const createApiRequest = <Handler>({
|
|
|
31
32
|
}: {
|
|
32
33
|
api: ApiRequest | ToddleApiV2<Handler>
|
|
33
34
|
formulaContext: FormulaContext
|
|
34
|
-
baseUrl?: string
|
|
35
|
+
baseUrl?: Nullable<string>
|
|
35
36
|
defaultHeaders: Headers | undefined
|
|
36
37
|
}) => {
|
|
37
38
|
const url = getUrl(api, formulaContext, baseUrl)
|
|
@@ -47,7 +48,7 @@ export const createApiRequest = <Handler>({
|
|
|
47
48
|
export const getUrl = (
|
|
48
49
|
api: ApiBase,
|
|
49
50
|
formulaContext: FormulaContext,
|
|
50
|
-
baseUrl?: string
|
|
51
|
+
baseUrl?: Nullable<string>,
|
|
51
52
|
): URL => {
|
|
52
53
|
let urlPathname = ''
|
|
53
54
|
let urlQueryParams = new URLSearchParams()
|
|
@@ -57,7 +58,7 @@ export const getUrl = (
|
|
|
57
58
|
const urlInput = typeof url === 'number' ? String(url) : url
|
|
58
59
|
try {
|
|
59
60
|
// Try to parse the URL to extract potential path and query parameters
|
|
60
|
-
parsedUrl = new URL(urlInput, baseUrl)
|
|
61
|
+
parsedUrl = new URL(urlInput, baseUrl ?? undefined)
|
|
61
62
|
urlPathname = parsedUrl.pathname
|
|
62
63
|
urlQueryParams = parsedUrl.searchParams
|
|
63
64
|
// eslint-disable-next-line no-empty
|
|
@@ -77,13 +78,13 @@ export const getUrl = (
|
|
|
77
78
|
const hashString =
|
|
78
79
|
typeof hash === 'string' && hash.length > 0 ? `#${hash}` : ''
|
|
79
80
|
if (parsedUrl) {
|
|
80
|
-
const combinedUrl = new URL(parsedUrl.origin, baseUrl)
|
|
81
|
+
const combinedUrl = new URL(parsedUrl.origin, baseUrl ?? undefined)
|
|
81
82
|
combinedUrl.pathname = path
|
|
82
83
|
combinedUrl.search = queryParams.toString()
|
|
83
84
|
combinedUrl.hash = hashString
|
|
84
85
|
return combinedUrl
|
|
85
86
|
} else {
|
|
86
|
-
return new URL(`${path}${queryString}${hashString}`, baseUrl)
|
|
87
|
+
return new URL(`${path}${queryString}${hashString}`, baseUrl ?? undefined)
|
|
87
88
|
}
|
|
88
89
|
}
|
|
89
90
|
|
|
@@ -224,7 +225,7 @@ export const getBaseUrl = ({
|
|
|
224
225
|
url,
|
|
225
226
|
}: {
|
|
226
227
|
origin: string
|
|
227
|
-
url?: string
|
|
228
|
+
url?: Nullable<string>
|
|
228
229
|
}) =>
|
|
229
230
|
!isDefined(url) || url === '' || url.startsWith('/') ? origin + url : url
|
|
230
231
|
|
|
@@ -254,13 +255,13 @@ export const isApiError = ({
|
|
|
254
255
|
apiName: string
|
|
255
256
|
response: {
|
|
256
257
|
ok: boolean
|
|
257
|
-
status?: number
|
|
258
|
-
headers?: Record<string, string
|
|
258
|
+
status?: Nullable<number>
|
|
259
|
+
headers?: Nullable<Record<string, string>>
|
|
259
260
|
body: unknown
|
|
260
261
|
}
|
|
261
262
|
formulaContext: FormulaContext
|
|
262
263
|
performance: ApiPerformance
|
|
263
|
-
errorFormula?: { formula: Formula }
|
|
264
|
+
errorFormula?: Nullable<{ formula: Formula }>
|
|
264
265
|
}) => {
|
|
265
266
|
const errorFormulaRes = errorFormula
|
|
266
267
|
? applyFormula(errorFormula.formula, {
|
package/src/api/apiTypes.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
import type { EventModel } from '../component/component.types'
|
|
2
2
|
import type { Formula } from '../formula/formula'
|
|
3
|
-
import type { NordcraftMetadata } from '../types'
|
|
3
|
+
import type { NordcraftMetadata, Nullable } from '../types'
|
|
4
4
|
|
|
5
5
|
export type ComponentAPI = LegacyComponentAPI | ApiRequest
|
|
6
6
|
|
|
7
7
|
export interface LegacyComponentAPI {
|
|
8
8
|
name: string
|
|
9
9
|
type: 'REST'
|
|
10
|
-
autoFetch?: Formula
|
|
11
|
-
url?: Formula
|
|
12
|
-
path?: { formula: Formula }[]
|
|
13
|
-
proxy?: boolean
|
|
14
|
-
queryParams?: Record<string, { name: string; formula: Formula }
|
|
15
|
-
headers?: Record<string, Formula> | Formula
|
|
16
|
-
method?: 'GET' | 'POST' | 'DELETE' | 'PUT'
|
|
17
|
-
body?: Formula
|
|
18
|
-
auth?: {
|
|
10
|
+
autoFetch?: Nullable<Formula>
|
|
11
|
+
url?: Nullable<Formula>
|
|
12
|
+
path?: Nullable<{ formula: Formula }[]>
|
|
13
|
+
proxy?: Nullable<boolean>
|
|
14
|
+
queryParams?: Nullable<Record<string, { name: string; formula: Formula }>>
|
|
15
|
+
headers?: Nullable<Record<string, Formula> | Formula>
|
|
16
|
+
method?: Nullable<'GET' | 'POST' | 'DELETE' | 'PUT'>
|
|
17
|
+
body?: Nullable<Formula>
|
|
18
|
+
auth?: Nullable<{
|
|
19
19
|
type: 'Bearer id_token' | 'Bearer access_token'
|
|
20
|
-
}
|
|
21
|
-
throttle?: number
|
|
22
|
-
debounce?: number
|
|
23
|
-
onCompleted
|
|
24
|
-
onFailed
|
|
20
|
+
}>
|
|
21
|
+
throttle?: Nullable<number>
|
|
22
|
+
debounce?: Nullable<number>
|
|
23
|
+
onCompleted?: Nullable<EventModel>
|
|
24
|
+
onFailed?: Nullable<EventModel>
|
|
25
25
|
version?: never
|
|
26
26
|
}
|
|
27
27
|
|
|
@@ -53,59 +53,60 @@ export type ApiParserMode =
|
|
|
53
53
|
| 'blob'
|
|
54
54
|
|
|
55
55
|
export interface ApiBase extends NordcraftMetadata {
|
|
56
|
-
url?: Formula
|
|
57
|
-
path?: Record<string, { formula: Formula; index: number }
|
|
58
|
-
queryParams?:
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
56
|
+
url?: Nullable<Formula>
|
|
57
|
+
path?: Nullable<Record<string, { formula: Formula; index: number }>>
|
|
58
|
+
queryParams?: Nullable<
|
|
59
|
+
Record<
|
|
60
|
+
string,
|
|
61
|
+
// The enabled formula is used to determine if the query parameter should be included in the request or not
|
|
62
|
+
{ formula: Formula; enabled?: Nullable<Formula> }
|
|
63
|
+
>
|
|
64
|
+
>
|
|
63
65
|
// hash is relevant for redirects and rewrites, but not for API calls
|
|
64
|
-
hash?: { formula: Formula }
|
|
66
|
+
hash?: Nullable<{ formula: Formula }>
|
|
65
67
|
}
|
|
66
68
|
|
|
67
69
|
export interface ApiRequest extends ApiBase {
|
|
68
70
|
version: 2
|
|
69
71
|
name: string
|
|
70
72
|
type: 'http' | 'ws' // The structure for web sockets might look different
|
|
71
|
-
autoFetch?: Formula
|
|
72
|
-
headers?:
|
|
73
|
-
string,
|
|
74
|
-
|
|
75
|
-
>
|
|
76
|
-
|
|
77
|
-
body?: Formula | null
|
|
73
|
+
autoFetch?: Nullable<Formula>
|
|
74
|
+
headers?: Nullable<
|
|
75
|
+
Record<string, { formula: Formula; enabled?: Nullable<Formula> }>
|
|
76
|
+
>
|
|
77
|
+
method?: Nullable<ApiMethod>
|
|
78
|
+
body?: Nullable<Formula>
|
|
78
79
|
// inputs for an API request
|
|
79
|
-
inputs: Record<string, { formula: Formula
|
|
80
|
-
service?: string
|
|
81
|
-
servicePath?: string
|
|
82
|
-
server?: {
|
|
80
|
+
inputs: Record<string, { formula: Nullable<Formula> }>
|
|
81
|
+
service?: Nullable<string>
|
|
82
|
+
servicePath?: Nullable<string>
|
|
83
|
+
server?: Nullable<{
|
|
83
84
|
// We should only accept server side proxy requests if proxy is defined
|
|
84
|
-
proxy?: {
|
|
85
|
+
proxy?: Nullable<{
|
|
85
86
|
enabled: { formula: Formula }
|
|
86
87
|
// Allow replacing template values in the body of a proxied request
|
|
87
88
|
// This is useful for cases where the body should include an http only cookie for instance
|
|
88
|
-
useTemplatesInBody?: { formula: Formula }
|
|
89
|
-
}
|
|
90
|
-
ssr?: {
|
|
89
|
+
useTemplatesInBody?: Nullable<{ formula: Formula }>
|
|
90
|
+
}>
|
|
91
|
+
ssr?: Nullable<{
|
|
91
92
|
// We should only fetch a request server side during SSR if this is true
|
|
92
93
|
// it should probably be true by default for autofetch APIs
|
|
93
94
|
// During SSR we will only fetch autoFetch requests
|
|
94
|
-
enabled?: { formula: Formula }
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
client?: {
|
|
98
|
-
debounce?: { formula: Formula }
|
|
99
|
-
onCompleted?: EventModel
|
|
100
|
-
onFailed?: EventModel
|
|
101
|
-
onMessage?: EventModel
|
|
95
|
+
enabled?: Nullable<{ formula: Formula }>
|
|
96
|
+
}>
|
|
97
|
+
}>
|
|
98
|
+
client?: Nullable<{
|
|
99
|
+
debounce?: Nullable<{ formula: Formula }>
|
|
100
|
+
onCompleted?: Nullable<EventModel>
|
|
101
|
+
onFailed?: Nullable<EventModel>
|
|
102
|
+
onMessage?: Nullable<EventModel>
|
|
102
103
|
// parserMode is used to determine how the response should be handled
|
|
103
104
|
// auto: The response will be handled based on the content type of the response
|
|
104
105
|
// stream: The response will be handled as a stream
|
|
105
106
|
parserMode: ApiParserMode
|
|
106
107
|
// Whether to include credentials (cookies) in the request or not. Default is 'same-origin'
|
|
107
|
-
credentials?: 'include' | 'same-origin' | 'omit'
|
|
108
|
-
}
|
|
108
|
+
credentials?: Nullable<'include' | 'same-origin' | 'omit'>
|
|
109
|
+
}>
|
|
109
110
|
// Shared logic for client/server 👇
|
|
110
111
|
// The user could distinguish using an environment
|
|
111
112
|
// variable e.g. IS_SERVER when they declare the formula
|
|
@@ -113,41 +114,43 @@ export interface ApiRequest extends ApiBase {
|
|
|
113
114
|
// Rules for redirecting the user to a different page
|
|
114
115
|
// These rules will run both on server+client - mostly used for 401 response -> 302 redirect
|
|
115
116
|
// We allow multiple rules since it makes it easier to setup multiple conditions/redirect locations
|
|
116
|
-
redirectRules?:
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
117
|
+
redirectRules?: Nullable<
|
|
118
|
+
Record<
|
|
119
|
+
string,
|
|
120
|
+
{
|
|
121
|
+
// The formula will receive the response from the server including a status code
|
|
122
|
+
// A redirect response will be returned if the formula returns a valid url
|
|
123
|
+
formula: Formula
|
|
124
|
+
// The status code used in the redirect response. Only relevant server side
|
|
125
|
+
statusCode?: Nullable<RedirectStatusCode>
|
|
126
|
+
index: number
|
|
127
|
+
}
|
|
128
|
+
>
|
|
129
|
+
>
|
|
127
130
|
// Formula for determining whether the response is an error or not. Receives the API response + status code/headers as input
|
|
128
131
|
// The response is considered an error if the formula returns true
|
|
129
132
|
// Default behavior is to check if the status code is 400 or higher
|
|
130
|
-
isError?: { formula: Formula }
|
|
133
|
+
isError?: Nullable<{ formula: Formula }>
|
|
131
134
|
// Formula for determining when the request should time out
|
|
132
|
-
timeout?: { formula: Formula }
|
|
135
|
+
timeout?: Nullable<{ formula: Formula }>
|
|
133
136
|
}
|
|
134
137
|
|
|
135
138
|
export interface ApiStatus {
|
|
136
139
|
data: unknown
|
|
137
140
|
isLoading: boolean
|
|
138
141
|
error: unknown
|
|
139
|
-
response?: {
|
|
140
|
-
status?: number
|
|
141
|
-
headers?: Record<string, string
|
|
142
|
-
performance?: ApiPerformance
|
|
143
|
-
debug?:
|
|
144
|
-
}
|
|
142
|
+
response?: Nullable<{
|
|
143
|
+
status?: Nullable<number>
|
|
144
|
+
headers?: Nullable<Record<string, string>>
|
|
145
|
+
performance?: Nullable<ApiPerformance>
|
|
146
|
+
debug?: Nullable<unknown>
|
|
147
|
+
}>
|
|
145
148
|
}
|
|
146
149
|
|
|
147
150
|
export interface ApiPerformance {
|
|
148
|
-
requestStart
|
|
149
|
-
responseStart
|
|
150
|
-
responseEnd
|
|
151
|
+
requestStart?: Nullable<number>
|
|
152
|
+
responseStart?: Nullable<number>
|
|
153
|
+
responseEnd?: Nullable<number>
|
|
151
154
|
}
|
|
152
155
|
|
|
153
156
|
export interface ToddleRequestInit extends RequestInit {
|
package/src/api/headers.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
+
import type { Nullable } from '../types'
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Checks if a header is a json (content-type) header
|
|
3
5
|
* Also supports edge cases like application/vnd.api+json and application/vnd.contentful.delivery.v1+json
|
|
4
6
|
* See https://jsonapi.org/#mime-types
|
|
5
7
|
*/
|
|
6
|
-
export const isJsonHeader = (header?: string
|
|
8
|
+
export const isJsonHeader = (header?: Nullable<string>) => {
|
|
7
9
|
if (typeof header !== 'string') {
|
|
8
10
|
return false
|
|
9
11
|
}
|
|
10
12
|
return /^application\/(json|.*\+json)/.test(header)
|
|
11
13
|
}
|
|
12
14
|
|
|
13
|
-
export const isTextHeader = (header?: string
|
|
15
|
+
export const isTextHeader = (header?: Nullable<string>) => {
|
|
14
16
|
if (typeof header !== 'string') {
|
|
15
17
|
return false
|
|
16
18
|
}
|
|
@@ -19,21 +21,21 @@ export const isTextHeader = (header?: string | null) => {
|
|
|
19
21
|
)
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
export const isEventStreamHeader = (header?: string
|
|
24
|
+
export const isEventStreamHeader = (header?: Nullable<string>) => {
|
|
23
25
|
if (typeof header !== 'string') {
|
|
24
26
|
return false
|
|
25
27
|
}
|
|
26
28
|
return /^text\/event-stream/.test(header)
|
|
27
29
|
}
|
|
28
30
|
|
|
29
|
-
export const isJsonStreamHeader = (header?: string
|
|
31
|
+
export const isJsonStreamHeader = (header?: Nullable<string>) => {
|
|
30
32
|
if (typeof header !== 'string') {
|
|
31
33
|
return false
|
|
32
34
|
}
|
|
33
35
|
return /^(application\/stream\+json|application\/x-ndjson)/.test(header)
|
|
34
36
|
}
|
|
35
37
|
|
|
36
|
-
export const isImageHeader = (header?: string
|
|
38
|
+
export const isImageHeader = (header?: Nullable<string>) => {
|
|
37
39
|
if (typeof header !== 'string') {
|
|
38
40
|
return false
|
|
39
41
|
}
|
|
@@ -69,11 +69,11 @@ export class ToddleComponent<Handler> {
|
|
|
69
69
|
globalFormulas: this.globalFormulas,
|
|
70
70
|
}),
|
|
71
71
|
)
|
|
72
|
-
Object.values(component.nodes).forEach(
|
|
72
|
+
Object.values(component.nodes ?? {}).forEach(
|
|
73
73
|
visitNode(node.package ?? packageName),
|
|
74
74
|
)
|
|
75
75
|
}
|
|
76
|
-
Object.values(this.nodes).forEach(visitNode())
|
|
76
|
+
Object.values(this.nodes ?? {}).forEach(visitNode())
|
|
77
77
|
return [...components.values()]
|
|
78
78
|
}
|
|
79
79
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { LegacyPluginAction, PluginActionV2 } from '../types'
|
|
1
|
+
import type { LegacyPluginAction, Nullable, PluginActionV2 } from '../types'
|
|
2
2
|
import { isDefined } from '../utils/util'
|
|
3
3
|
import type { ActionModel } from './component.types'
|
|
4
4
|
|
|
5
5
|
export function* getActionsInAction(
|
|
6
|
-
action: ActionModel
|
|
6
|
+
action: Nullable<ActionModel>,
|
|
7
7
|
path: (string | number)[] = [],
|
|
8
8
|
): Generator<[(string | number)[], ActionModel]> {
|
|
9
9
|
if (!isDefined(action)) {
|
|
@@ -32,6 +32,7 @@ export function* getActionsInAction(
|
|
|
32
32
|
break
|
|
33
33
|
case 'Custom':
|
|
34
34
|
case undefined:
|
|
35
|
+
case null:
|
|
35
36
|
for (const [eventKey, event] of Object.entries(action.events ?? {})) {
|
|
36
37
|
for (const [key, a] of Object.entries(event?.actions ?? {})) {
|
|
37
38
|
yield* getActionsInAction(a, [
|