@sanity/blueprints 0.27.0 → 0.28.0
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 +22 -0
- package/dist/definers/functions/durable.d.ts +0 -1
- package/dist/definers/functions/durable.js +3 -4
- package/dist/definers/functions/durable.js.map +1 -1
- package/dist/definers/functions/queue.js +3 -3
- package/dist/definers/functions/queue.js.map +1 -1
- package/dist/resolve/by-name.d.ts +7 -0
- package/dist/resolve/by-name.js +13 -0
- package/dist/resolve/by-name.js.map +1 -0
- package/dist/resolve/import.d.ts +16 -0
- package/dist/resolve/import.js +48 -0
- package/dist/resolve/import.js.map +1 -0
- package/dist/resolve/index.d.ts +18 -0
- package/dist/resolve/index.js +19 -0
- package/dist/resolve/index.js.map +1 -0
- package/dist/resolve/load.d.ts +53 -0
- package/dist/resolve/load.js +155 -0
- package/dist/resolve/load.js.map +1 -0
- package/dist/resolve/stack-config.d.ts +49 -0
- package/dist/resolve/stack-config.js +108 -0
- package/dist/resolve/stack-config.js.map +1 -0
- package/dist/resolve/stack.d.ts +23 -0
- package/dist/resolve/stack.js +91 -0
- package/dist/resolve/stack.js.map +1 -0
- package/dist/resolve/types.d.ts +165 -0
- package/dist/resolve/types.js +2 -0
- package/dist/resolve/types.js.map +1 -0
- package/dist/types/functions/index.d.ts +58 -10
- package/dist/utils/debounce.d.ts +20 -0
- package/dist/utils/debounce.js +46 -0
- package/dist/utils/debounce.js.map +1 -0
- package/dist/validation/functions.d.ts +9 -0
- package/dist/validation/functions.js +66 -9
- package/dist/validation/functions.js.map +1 -1
- package/package.json +16 -7
- package/src/definers/functions/durable.ts +3 -4
- package/src/definers/functions/queue.ts +3 -3
- package/src/resolve/by-name.ts +11 -0
- package/src/resolve/import.ts +52 -0
- package/src/resolve/index.ts +33 -0
- package/src/resolve/load.ts +166 -0
- package/src/resolve/stack-config.ts +116 -0
- package/src/resolve/stack.ts +100 -0
- package/src/resolve/types.ts +179 -0
- package/src/types/functions/index.ts +70 -19
- package/src/utils/debounce.ts +53 -0
- package/src/validation/functions.ts +78 -12
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import type {Blueprint, BlueprintModule, BlueprintResource, BlueprintsApiConfig} from '../index.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The scope a Stack lives in
|
|
5
|
+
* @category Resolve
|
|
6
|
+
*/
|
|
7
|
+
export type ScopeType = 'organization' | 'project'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Where a resolved id came from, highest precedence first
|
|
11
|
+
*
|
|
12
|
+
* - `overrides`: passed by the caller, for example from command line flags
|
|
13
|
+
* - `env`: the `SANITY_ORGANIZATION_ID`, `SANITY_PROJECT_ID`, and `SANITY_BLUEPRINT_STACK_ID` environment variables
|
|
14
|
+
* - `module`: attached to the blueprint module by `defineBlueprint`
|
|
15
|
+
* - `config`: `.sanity/blueprint.config.json` next to the blueprint file
|
|
16
|
+
* @category Resolve
|
|
17
|
+
*/
|
|
18
|
+
export type ConfigSource = 'overrides' | 'env' | 'module' | 'config'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The ids that identify a Stack
|
|
22
|
+
* @category Resolve
|
|
23
|
+
*/
|
|
24
|
+
export type StackIds = Partial<BlueprintsApiConfig>
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The ids from each source, in the order {@link resolveStackConfig} checks them
|
|
28
|
+
* @internal
|
|
29
|
+
* @private
|
|
30
|
+
* @category Resolve
|
|
31
|
+
*/
|
|
32
|
+
export interface StackIdSources {
|
|
33
|
+
overrides?: StackIds | null
|
|
34
|
+
/** Defaults to the `SANITY_*` environment variables. Pass `null` to skip them. */
|
|
35
|
+
env?: StackIds | null
|
|
36
|
+
module?: StackIds | null
|
|
37
|
+
config?: StackIds | null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The Stack ids and scope, with the source of each id
|
|
42
|
+
* @category Resolve
|
|
43
|
+
*/
|
|
44
|
+
export interface StackConfig extends StackIds {
|
|
45
|
+
/** `project` when a project id is known, else `organization` when an organization id is known */
|
|
46
|
+
scopeType?: ScopeType
|
|
47
|
+
scopeId?: string
|
|
48
|
+
/** Which source supplied each id */
|
|
49
|
+
sources: {[K in keyof BlueprintsApiConfig]?: ConfigSource}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The ids in `.sanity/blueprint.config.json`, and the path of that file
|
|
54
|
+
* @internal
|
|
55
|
+
* @private
|
|
56
|
+
* @category Resolve
|
|
57
|
+
*/
|
|
58
|
+
export interface StackConfigFile extends StackIds {
|
|
59
|
+
configPath: string
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Imports a blueprint file and returns its default export
|
|
64
|
+
* @category Resolve
|
|
65
|
+
*/
|
|
66
|
+
export type BlueprintLoader = (path: string) => Promise<unknown>
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Options for loading a blueprint file
|
|
70
|
+
* @category Resolve
|
|
71
|
+
*/
|
|
72
|
+
export interface LoadBlueprintFileOptions {
|
|
73
|
+
/** Imports a JS or TS blueprint file in place of the built-in loading, for example to skip the module cache */
|
|
74
|
+
loader?: BlueprintLoader
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A blueprint file as loaded from disk
|
|
79
|
+
* @category Resolve
|
|
80
|
+
*/
|
|
81
|
+
export interface LoadedBlueprintFile {
|
|
82
|
+
/** The blueprint document the module returned or the JSON file held */
|
|
83
|
+
blueprint: Blueprint
|
|
84
|
+
/** The default export of a JS or TS file. Absent when the file was JSON. */
|
|
85
|
+
module?: BlueprintModule
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Options for {@link readBlueprint}
|
|
90
|
+
* @category Resolve
|
|
91
|
+
*/
|
|
92
|
+
export interface ReadBlueprintOptions extends LoadBlueprintFileOptions {
|
|
93
|
+
/** A blueprint file or directory. Defaults to the current working directory. */
|
|
94
|
+
path?: string
|
|
95
|
+
/** Ids that win over every other source */
|
|
96
|
+
overrides?: StackIds | null
|
|
97
|
+
/** The environment to read. Defaults to `process.env`. Pass `null` to skip environment variables. */
|
|
98
|
+
env?: NodeJS.ProcessEnv | null
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* What a blueprint declares, and the ids that say which Stack it deploys to
|
|
103
|
+
* @category Resolve
|
|
104
|
+
*/
|
|
105
|
+
export interface BlueprintInfo extends StackConfig, LoadedBlueprintFile {
|
|
106
|
+
/** The absolute path of the blueprint file */
|
|
107
|
+
path: string
|
|
108
|
+
/** `blueprint.resources`, or an empty list */
|
|
109
|
+
resources: BlueprintResource[]
|
|
110
|
+
/** `resources` keyed by name. A later duplicate name wins. */
|
|
111
|
+
resourcesByName: Record<string, BlueprintResource>
|
|
112
|
+
/** The `.sanity/blueprint.config.json` that was read, when one exists */
|
|
113
|
+
configPath?: string
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* A resource as the Blueprints API returns it
|
|
118
|
+
* @category Resolve
|
|
119
|
+
*/
|
|
120
|
+
export interface DeployedResource {
|
|
121
|
+
id: string
|
|
122
|
+
name: string
|
|
123
|
+
type: string
|
|
124
|
+
externalId?: string | null
|
|
125
|
+
parameters?: Record<string, unknown>
|
|
126
|
+
/** Parameters with references to other resources resolved */
|
|
127
|
+
resolvedParameters?: Record<string, unknown>
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* A Stack as the Blueprints API returns it
|
|
132
|
+
* @category Resolve
|
|
133
|
+
*/
|
|
134
|
+
export interface DeployedStack {
|
|
135
|
+
id: string
|
|
136
|
+
name: string
|
|
137
|
+
scopeType: ScopeType
|
|
138
|
+
scopeId: string
|
|
139
|
+
defaultProjectId?: string | null
|
|
140
|
+
resources: DeployedResource[]
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A declared resource with its deployed values, when the Stack has a resource of the same type and name
|
|
145
|
+
* @category Resolve
|
|
146
|
+
*/
|
|
147
|
+
export type LiveResource = BlueprintResource & {
|
|
148
|
+
/** The id of the resource in the service that owns it */
|
|
149
|
+
externalId?: string
|
|
150
|
+
/** The deployed parameters, with references resolved when the API has them */
|
|
151
|
+
live?: Record<string, unknown>
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* A blueprint with the values its deployed Stack holds
|
|
156
|
+
* @category Resolve
|
|
157
|
+
*/
|
|
158
|
+
export interface StackInfo extends BlueprintInfo {
|
|
159
|
+
stack: DeployedStack
|
|
160
|
+
resources: LiveResource[]
|
|
161
|
+
resourcesByName: Record<string, LiveResource>
|
|
162
|
+
/** `live` parameters by resource name, for the resources the Stack has deployed */
|
|
163
|
+
values: Record<string, Record<string, unknown>>
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Options for {@link readStack}
|
|
168
|
+
* @category Resolve
|
|
169
|
+
*/
|
|
170
|
+
export interface ReadStackOptions extends ReadBlueprintOptions {
|
|
171
|
+
/** A Sanity auth token */
|
|
172
|
+
token: string
|
|
173
|
+
/** Defaults to the global `fetch` */
|
|
174
|
+
fetch?: typeof fetch
|
|
175
|
+
/** The Sanity API origin. Defaults from `SANITY_INTERNAL_ENV`. */
|
|
176
|
+
apiUrl?: string
|
|
177
|
+
/** The Blueprints API version. Defaults to `SANITY_BLUEPRINTS_API_VERSION` or {@link DEFAULT_BLUEPRINTS_API_VERSION}. */
|
|
178
|
+
apiVersion?: string
|
|
179
|
+
}
|
|
@@ -63,15 +63,10 @@ interface QueueConfig {
|
|
|
63
63
|
*/
|
|
64
64
|
concurrency?: number
|
|
65
65
|
/**
|
|
66
|
-
* Debounce
|
|
67
|
-
*
|
|
68
|
-
*/
|
|
69
|
-
debounce?: number
|
|
70
|
-
/**
|
|
71
|
-
* Path used to group debounced events, e.g. 'document._id'
|
|
72
|
-
* @todo: not implemented - and should this always be provided with debounce window?
|
|
66
|
+
* Debounce configuration, with any durations already resolved to seconds.
|
|
67
|
+
* Definers accept the wider {@link DebounceInput} and parse it into this shape.
|
|
73
68
|
*/
|
|
74
|
-
|
|
69
|
+
debounce?: DebounceConfig
|
|
75
70
|
/**
|
|
76
71
|
* Whether to place messages that failed processing into a Dead Letter Queue.
|
|
77
72
|
* @todo: not implemented
|
|
@@ -87,6 +82,56 @@ interface QueueConfig {
|
|
|
87
82
|
fifo?: boolean
|
|
88
83
|
}
|
|
89
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Configuration used when debouncing Queue or Workflow function types.
|
|
87
|
+
* @category Functions Types
|
|
88
|
+
* @alpha
|
|
89
|
+
* @hidden
|
|
90
|
+
*/
|
|
91
|
+
export interface DebounceConfig {
|
|
92
|
+
/**
|
|
93
|
+
* Debounce window in seconds
|
|
94
|
+
*/
|
|
95
|
+
window: number
|
|
96
|
+
/**
|
|
97
|
+
* Maximum time in seconds to extend a debounce window from the first event
|
|
98
|
+
*/
|
|
99
|
+
maxWindow?: number
|
|
100
|
+
/**
|
|
101
|
+
* Dotted path into {event, context} used to debounce events
|
|
102
|
+
*/
|
|
103
|
+
key?: string
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Debounce configuration as accepted by a function definer, before durations are parsed.
|
|
108
|
+
* @category Functions Types
|
|
109
|
+
* @alpha
|
|
110
|
+
* @hidden
|
|
111
|
+
*/
|
|
112
|
+
export interface DebounceConfigInput extends Omit<DebounceConfig, 'window' | 'maxWindow'> {
|
|
113
|
+
/**
|
|
114
|
+
* Debounce window
|
|
115
|
+
* A number is read as seconds, a string is parsed as a duration, e.g. `'5 minutes'`.
|
|
116
|
+
*/
|
|
117
|
+
window: string | number
|
|
118
|
+
/**
|
|
119
|
+
* Maximum time to extend a debounce window from the first event.
|
|
120
|
+
* Must be greater than `window`.
|
|
121
|
+
* A number is read as seconds, a string is parsed as a duration, e.g. `'1 hour'`.
|
|
122
|
+
*/
|
|
123
|
+
maxWindow?: string | number
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Debounce configuration accepted by a function definer, either as a bare duration
|
|
128
|
+
* used as the window, or as a full config.
|
|
129
|
+
* @category Functions Types
|
|
130
|
+
* @alpha
|
|
131
|
+
* @hidden
|
|
132
|
+
*/
|
|
133
|
+
export type DebounceInput = string | number | DebounceConfigInput
|
|
134
|
+
|
|
90
135
|
/**
|
|
91
136
|
* Base function resource with common properties for all function types that can belong to projects.
|
|
92
137
|
* @category Functions Types
|
|
@@ -262,17 +307,19 @@ export type BlueprintSyncTagInvalidateFunctionConfig = Omit<BlueprintSyncTagInva
|
|
|
262
307
|
* @category Functions Types
|
|
263
308
|
* @interface
|
|
264
309
|
*/
|
|
265
|
-
export type BlueprintQueueFunctionConfig = Omit<BlueprintQueueFunctionResource, 'type' | 'src' | 'event'> &
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
src?: string
|
|
310
|
+
export type BlueprintQueueFunctionConfig = Omit<BlueprintQueueFunctionResource, 'type' | 'src' | 'event' | 'debounce'> & {
|
|
311
|
+
/**
|
|
312
|
+
* Path to the function source code
|
|
313
|
+
* @defaultValue `functions/${name}`
|
|
314
|
+
*/
|
|
315
|
+
src?: string
|
|
272
316
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
317
|
+
/** Optional event configuration that triggers the queue function */
|
|
318
|
+
event?: BlueprintFunctionResourceContentLakeEvent
|
|
319
|
+
|
|
320
|
+
/** Optional debounce configuration, as a duration or a full config */
|
|
321
|
+
debounce?: DebounceInput
|
|
322
|
+
}
|
|
276
323
|
|
|
277
324
|
/**
|
|
278
325
|
* Configuration for defining an event function.
|
|
@@ -296,7 +343,7 @@ export type BlueprintPubSubFunctionConfig = Omit<BlueprintPubSubFunctionResource
|
|
|
296
343
|
* @category Functions Types
|
|
297
344
|
* @interface
|
|
298
345
|
*/
|
|
299
|
-
export type BlueprintDurableConfig = Omit<BlueprintDurableFunctionResource, 'type' | 'src' | 'event' | 'durableTimeout'> & {
|
|
346
|
+
export type BlueprintDurableConfig = Omit<BlueprintDurableFunctionResource, 'type' | 'src' | 'event' | 'durableTimeout' | 'debounce'> & {
|
|
300
347
|
/**
|
|
301
348
|
* Path to the function source code
|
|
302
349
|
* @defaultValue `functions/${name}`
|
|
@@ -311,4 +358,8 @@ export type BlueprintDurableConfig = Omit<BlueprintDurableFunctionResource, 'typ
|
|
|
311
358
|
* @defaultValue 86_400 (24 hours)
|
|
312
359
|
*/
|
|
313
360
|
durableTimeout?: string | number
|
|
361
|
+
/**
|
|
362
|
+
* Optional debounce configuration, as a duration or a full config
|
|
363
|
+
*/
|
|
364
|
+
debounce?: DebounceInput
|
|
314
365
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type {BlueprintError} from '../types/errors.js'
|
|
2
|
+
import type {DebounceConfig, DebounceInput} from '../types/functions/index.js'
|
|
3
|
+
import {parseDuration} from './parse-duration.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Normalizes the debounce config accepted by a definer into the shape held by a resource,
|
|
7
|
+
* parsing any duration into seconds. A bare duration is shorthand for `{window: <duration>}`.
|
|
8
|
+
* @param debounce The debounce config or duration to normalize
|
|
9
|
+
* @internal
|
|
10
|
+
* @returns The debounce config, with `window` and `maxWindow` in seconds
|
|
11
|
+
*/
|
|
12
|
+
export function createDebounceObject(debounce: DebounceInput): DebounceConfig {
|
|
13
|
+
if (typeof debounce !== 'object') {
|
|
14
|
+
return {window: parseDuration(debounce, 's')}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const {window, maxWindow, key} = debounce
|
|
18
|
+
|
|
19
|
+
// `window` is required, so there is nothing sensible to parse without it
|
|
20
|
+
if (window === undefined) {
|
|
21
|
+
throw new Error('Invalid debounce config: `window` must be provided')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
window: parseDuration(window, 's'),
|
|
26
|
+
...(maxWindow !== undefined && {maxWindow: parseDuration(maxWindow, 's')}),
|
|
27
|
+
...(key !== undefined && {key}),
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Resolves a debounce duration, given either as a number of seconds or as a duration string, to seconds.
|
|
33
|
+
* @param field The name of the field being resolved, used in error messages
|
|
34
|
+
* @param value The value to resolve
|
|
35
|
+
* @returns The value in seconds, or the errors that prevented it from being resolved
|
|
36
|
+
*/
|
|
37
|
+
export function resolveDebounceDuration(field: string, value: unknown): {seconds?: number; errors: BlueprintError[]} {
|
|
38
|
+
if (typeof value === 'number') {
|
|
39
|
+
return Number.isFinite(value)
|
|
40
|
+
? {seconds: value, errors: []}
|
|
41
|
+
: {errors: [{type: 'invalid_value', message: `\`${field}\` must be a valid duration`}]}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (typeof value === 'string') {
|
|
45
|
+
try {
|
|
46
|
+
return {seconds: parseDuration(value, 's'), errors: []}
|
|
47
|
+
} catch {
|
|
48
|
+
return {errors: [{type: 'invalid_value', message: `\`${field}\` must be a valid duration`}]}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return {errors: [{type: 'invalid_type', message: `\`${field}\` must be a number of seconds or a duration string`}]}
|
|
53
|
+
}
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
VALID_RUNTIMES,
|
|
8
8
|
validateResource,
|
|
9
9
|
} from '../index.js'
|
|
10
|
+
import {resolveDebounceDuration} from '../utils/debounce.js'
|
|
10
11
|
import {isReference} from '../utils/validation.js'
|
|
11
12
|
|
|
12
13
|
type BaseFunctionEventKey = keyof BlueprintFunctionBaseResourceEvent
|
|
@@ -469,6 +470,10 @@ export function validateQueueFunction(functionResource: unknown): BlueprintError
|
|
|
469
470
|
errors.push(...validateFunctionContentLakeEvent(functionResource.event))
|
|
470
471
|
}
|
|
471
472
|
|
|
473
|
+
if ('debounce' in functionResource) {
|
|
474
|
+
errors.push(...validateDebounceConfig(functionResource.debounce))
|
|
475
|
+
}
|
|
476
|
+
|
|
472
477
|
errors.push(...validateFunction(functionResource))
|
|
473
478
|
|
|
474
479
|
return errors
|
|
@@ -543,14 +548,6 @@ export function validateDurableFunction(functionResource: unknown): BlueprintErr
|
|
|
543
548
|
errors.push({type: 'invalid_value', message: '`type` must be `sanity.function.durable`'})
|
|
544
549
|
}
|
|
545
550
|
|
|
546
|
-
if ('debounceKey' in functionResource && typeof functionResource.debounceKey !== 'string') {
|
|
547
|
-
errors.push({type: 'invalid_type', message: '`debounceKey` must be a string'})
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
if ('debounceKey' in functionResource && !('debounce' in functionResource)) {
|
|
551
|
-
errors.push({type: 'invalid_value', message: '`debounceKey` requires a `debounce` to be set'})
|
|
552
|
-
}
|
|
553
|
-
|
|
554
551
|
if ('concurrency' in functionResource && typeof functionResource.concurrency !== 'number') {
|
|
555
552
|
errors.push({type: 'invalid_type', message: '`concurrency` must be a number'})
|
|
556
553
|
}
|
|
@@ -563,10 +560,6 @@ export function validateDurableFunction(functionResource: unknown): BlueprintErr
|
|
|
563
560
|
errors.push({type: 'invalid_value', message: '`concurrency` must be less than 500'})
|
|
564
561
|
}
|
|
565
562
|
|
|
566
|
-
if ('debounce' in functionResource && typeof functionResource.debounce !== 'number') {
|
|
567
|
-
errors.push({type: 'invalid_type', message: '`debounce` must be a number'})
|
|
568
|
-
}
|
|
569
|
-
|
|
570
563
|
if ('durableTimeout' in functionResource && typeof functionResource.durableTimeout !== 'number') {
|
|
571
564
|
errors.push({type: 'invalid_type', message: '`durableTimeout` must be a number'})
|
|
572
565
|
}
|
|
@@ -584,7 +577,80 @@ export function validateDurableFunction(functionResource: unknown): BlueprintErr
|
|
|
584
577
|
errors.push(...validateFunctionContentLakeEvent(functionResource.event))
|
|
585
578
|
}
|
|
586
579
|
|
|
580
|
+
if ('debounce' in functionResource) {
|
|
581
|
+
errors.push(...validateDebounceConfig(functionResource.debounce))
|
|
582
|
+
}
|
|
583
|
+
|
|
587
584
|
errors.push(...validateFunction(functionResource))
|
|
588
585
|
|
|
589
586
|
return errors
|
|
590
587
|
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Validates a debounce config.
|
|
591
|
+
* @param debounce The config to validate
|
|
592
|
+
* @alpha
|
|
593
|
+
* @hidden
|
|
594
|
+
* @category Functions Types
|
|
595
|
+
* @returns Array of validation errors, empty if valid
|
|
596
|
+
*/
|
|
597
|
+
export function validateDebounceConfig(debounce: unknown): BlueprintError[] {
|
|
598
|
+
if (debounce === undefined || debounce === null) {
|
|
599
|
+
return [{type: 'invalid_value', message: 'Debounce config must be provided'}]
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// A bare duration is shorthand for `{window: <duration>}`
|
|
603
|
+
if (typeof debounce === 'string' || typeof debounce === 'number') {
|
|
604
|
+
const resolved = resolveDebounceDuration('debounce', debounce)
|
|
605
|
+
return resolved.seconds === undefined ? resolved.errors : validateDebounceConfig({window: resolved.seconds})
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
if (typeof debounce !== 'object') return [{type: 'invalid_type', message: 'Debounce config must be an object'}]
|
|
609
|
+
|
|
610
|
+
const errors: BlueprintError[] = []
|
|
611
|
+
|
|
612
|
+
// each window is only kept when it is individually valid, so the comparison below doesn't pile onto an existing error
|
|
613
|
+
let window: number | undefined
|
|
614
|
+
let maxWindow: number | undefined
|
|
615
|
+
|
|
616
|
+
if (!('window' in debounce)) {
|
|
617
|
+
errors.push({type: 'missing_parameter', message: '`window` must be provided'})
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
if ('window' in debounce) {
|
|
621
|
+
const resolved = resolveDebounceDuration('window', debounce.window)
|
|
622
|
+
errors.push(...resolved.errors)
|
|
623
|
+
|
|
624
|
+
if (resolved.seconds !== undefined) {
|
|
625
|
+
if (resolved.seconds < 1) {
|
|
626
|
+
errors.push({type: 'invalid_value', message: '`window` must be greater than 1 second'})
|
|
627
|
+
} else {
|
|
628
|
+
window = resolved.seconds
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
if ('maxWindow' in debounce) {
|
|
634
|
+
const resolved = resolveDebounceDuration('maxWindow', debounce.maxWindow)
|
|
635
|
+
errors.push(...resolved.errors)
|
|
636
|
+
|
|
637
|
+
if (resolved.seconds !== undefined) {
|
|
638
|
+
if (resolved.seconds < 1) {
|
|
639
|
+
errors.push({type: 'invalid_value', message: '`maxWindow` must be greater than 1 second'})
|
|
640
|
+
} else {
|
|
641
|
+
maxWindow = resolved.seconds
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// `maxWindow` caps how far `window` can be extended from the first event, so it has to leave room to extend
|
|
647
|
+
if (window !== undefined && maxWindow !== undefined && maxWindow <= window) {
|
|
648
|
+
errors.push({type: 'invalid_value', message: '`maxWindow` must be greater than `window`'})
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
if ('key' in debounce && typeof debounce.key !== 'string') {
|
|
652
|
+
errors.push({type: 'invalid_type', message: '`key` must be a string'})
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
return errors
|
|
656
|
+
}
|