@sanity/functions 1.3.0 → 1.4.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/dist/definers.d.ts +9 -2
- package/dist/definers.js +12 -1
- package/dist/definers.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/invoke.d.ts +2 -0
- package/dist/invoke.js +90 -0
- package/dist/invoke.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +79 -24
- package/package.json +25 -6
- package/src/aws-lite-plugins.d.ts +4 -0
- package/src/definers.ts +13 -2
- package/src/index.ts +1 -0
- package/src/invoke.ts +95 -0
- package/src/types.ts +83 -24
package/dist/definers.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DocumentEventHandler, ScheduledEventHandler, SyncTagInvalidateEventHandler } from './types.js';
|
|
1
|
+
import type { DocumentEventHandler, EventHandler, ScheduledEventHandler, SyncTagInvalidateEventHandler } from './types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Defines a "document event" function handler.
|
|
4
4
|
* Returns the handler function as-is, only providing the types and doing basic validation.
|
|
@@ -11,7 +11,6 @@ export declare function documentEventHandler<IData = any>(handler: DocumentEvent
|
|
|
11
11
|
* Defines a "scheduled event" function handler.
|
|
12
12
|
* Returns the handler function as-is, only providing the types and doing basic validation.
|
|
13
13
|
*
|
|
14
|
-
* @beta
|
|
15
14
|
* @param handler - The event handler function to use.
|
|
16
15
|
* @returns The handler function, unmodified.
|
|
17
16
|
*/
|
|
@@ -24,3 +23,11 @@ export declare function scheduledEventHandler(handler: ScheduledEventHandler): S
|
|
|
24
23
|
* @returns The handler function, unmodified.
|
|
25
24
|
*/
|
|
26
25
|
export declare function syncTagInvalidateEventHandler(handler: SyncTagInvalidateEventHandler): SyncTagInvalidateEventHandler;
|
|
26
|
+
/**
|
|
27
|
+
* Defines a "generic" function handler.
|
|
28
|
+
* Returns the handler function as-is, only providing the types and doing basic validation.
|
|
29
|
+
*
|
|
30
|
+
* @param handler - The event handler function to use.
|
|
31
|
+
* @returns The handler function, unmodified.
|
|
32
|
+
*/
|
|
33
|
+
export declare function eventHandler<IData = any>(handler: EventHandler<IData>): EventHandler<IData>;
|
package/dist/definers.js
CHANGED
|
@@ -14,7 +14,6 @@ export function documentEventHandler(handler) {
|
|
|
14
14
|
* Defines a "scheduled event" function handler.
|
|
15
15
|
* Returns the handler function as-is, only providing the types and doing basic validation.
|
|
16
16
|
*
|
|
17
|
-
* @beta
|
|
18
17
|
* @param handler - The event handler function to use.
|
|
19
18
|
* @returns The handler function, unmodified.
|
|
20
19
|
*/
|
|
@@ -35,4 +34,16 @@ export function syncTagInvalidateEventHandler(handler) {
|
|
|
35
34
|
throw new TypeError('`handler` must be a function');
|
|
36
35
|
return handler;
|
|
37
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Defines a "generic" function handler.
|
|
39
|
+
* Returns the handler function as-is, only providing the types and doing basic validation.
|
|
40
|
+
*
|
|
41
|
+
* @param handler - The event handler function to use.
|
|
42
|
+
* @returns The handler function, unmodified.
|
|
43
|
+
*/
|
|
44
|
+
export function eventHandler(handler) {
|
|
45
|
+
if (typeof handler !== 'function')
|
|
46
|
+
throw new TypeError('`handler` must be a function');
|
|
47
|
+
return handler;
|
|
48
|
+
}
|
|
38
49
|
//# sourceMappingURL=definers.js.map
|
package/dist/definers.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"definers.js","sourceRoot":"","sources":["../src/definers.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAc,OAAoC;IACpF,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC;AAED
|
|
1
|
+
{"version":3,"file":"definers.js","sourceRoot":"","sources":["../src/definers.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAc,OAAoC;IACpF,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CAAC,OAA8B;IAClE,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,6BAA6B,CAAC,OAAsC;IAClF,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAc,OAA4B;IACpE,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA"}
|
package/dist/invoke.d.ts
ADDED
package/dist/invoke.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { env } from 'node:process';
|
|
3
|
+
const MAX_EVENT_SIZE_BYTES = 256 * 1024;
|
|
4
|
+
let awsPromise;
|
|
5
|
+
const PARTITION_KEY = 'arc-app-res';
|
|
6
|
+
/**
|
|
7
|
+
* lazy load aws-lite
|
|
8
|
+
*/
|
|
9
|
+
function getAwsLite() {
|
|
10
|
+
if (!awsPromise) {
|
|
11
|
+
awsPromise = (async () => {
|
|
12
|
+
const { default: awsLite } = await import('@aws-lite/client');
|
|
13
|
+
return awsLite({
|
|
14
|
+
plugins: [import('@aws-lite/dynamodb'), import('@aws-lite/lambda'), import('@aws-lite/sns'), import('@aws-lite/sqs')],
|
|
15
|
+
});
|
|
16
|
+
})().catch((err) => {
|
|
17
|
+
awsPromise = undefined;
|
|
18
|
+
throw err;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return awsPromise;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Gets a specific resource based off of function name
|
|
25
|
+
* @param {string} name
|
|
26
|
+
*/
|
|
27
|
+
async function getResource(name, aws) {
|
|
28
|
+
const TableName = env['SANITY_DISCO'];
|
|
29
|
+
if (!TableName)
|
|
30
|
+
throw new Error('SANITY_DISCO env var not set');
|
|
31
|
+
const result = await aws.DynamoDB.GetItem({
|
|
32
|
+
TableName,
|
|
33
|
+
Key: {
|
|
34
|
+
PK: PARTITION_KEY,
|
|
35
|
+
SK: name,
|
|
36
|
+
},
|
|
37
|
+
ProjectionExpression: 'resources',
|
|
38
|
+
});
|
|
39
|
+
if (!result?.Item)
|
|
40
|
+
throw new Error(`Function not found: ${name}`);
|
|
41
|
+
if (!result.Item['resources'])
|
|
42
|
+
throw new Error(`Resource record for ${name} is missing resources`);
|
|
43
|
+
return result.Item['resources'];
|
|
44
|
+
}
|
|
45
|
+
export async function invoke(name, payload) {
|
|
46
|
+
if (!name)
|
|
47
|
+
throw new Error('Function name was not provided');
|
|
48
|
+
const stringPayload = JSON.stringify(payload);
|
|
49
|
+
// Check to make sure payload is not over the max we can handle
|
|
50
|
+
if (Buffer.byteLength(stringPayload, 'utf8') > MAX_EVENT_SIZE_BYTES) {
|
|
51
|
+
throw new Error(`Payload exceeds maximum size of ${MAX_EVENT_SIZE_BYTES / 1024}KB`);
|
|
52
|
+
}
|
|
53
|
+
// Local invoke path for Sanity CLI
|
|
54
|
+
if (payload?.context?.local) {
|
|
55
|
+
if (!payload?.context?.invoke) {
|
|
56
|
+
throw new Error(`No local invoke handler configured for function: ${name}`);
|
|
57
|
+
}
|
|
58
|
+
payload.context.invoke(name, payload);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const aws = await getAwsLite();
|
|
62
|
+
if (!aws)
|
|
63
|
+
throw new Error(`Unable to invoke function: ${name}`);
|
|
64
|
+
// Look up the function details
|
|
65
|
+
const resource = await getResource(name, aws);
|
|
66
|
+
// Determine which method to invoke the function
|
|
67
|
+
if (resource.topic) {
|
|
68
|
+
await aws.SNS.Publish({
|
|
69
|
+
TopicArn: resource.topic.physicalResourceId,
|
|
70
|
+
Message: stringPayload,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
else if (resource.queue) {
|
|
74
|
+
await aws.SQS.SendMessage({
|
|
75
|
+
MessageBody: stringPayload,
|
|
76
|
+
QueueUrl: resource.queue.physicalResourceId,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
else if (resource.function) {
|
|
80
|
+
await aws.Lambda.Invoke({
|
|
81
|
+
FunctionName: resource.function.physicalResourceId,
|
|
82
|
+
Payload: payload,
|
|
83
|
+
InvocationType: 'Event',
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
throw new Error(`No invokeable resource for function: ${name}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=invoke.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invoke.js","sourceRoot":"","sources":["../src/invoke.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,aAA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