@zapier/zapier-sdk 0.4.1 → 0.5.1
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/CHANGELOG.md +13 -0
- package/README.md +343 -0
- package/dist/api/auth.d.ts +9 -0
- package/dist/api/auth.d.ts.map +1 -0
- package/dist/api/auth.js +25 -0
- package/dist/api/client.d.ts +9 -0
- package/dist/api/client.d.ts.map +1 -0
- package/dist/api/client.js +322 -0
- package/dist/api/debug.d.ts +13 -0
- package/dist/api/debug.d.ts.map +1 -0
- package/dist/api/debug.js +55 -0
- package/dist/api/index.d.ts +29 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +41 -0
- package/dist/api/polling.d.ts +16 -0
- package/dist/api/polling.d.ts.map +1 -0
- package/dist/api/polling.js +45 -0
- package/dist/api/schemas.d.ts +2473 -0
- package/dist/api/schemas.d.ts.map +1 -0
- package/dist/api/schemas.js +355 -0
- package/dist/api/types.d.ts +75 -0
- package/dist/api/types.d.ts.map +1 -0
- package/dist/api/types.js +11 -0
- package/dist/auth.d.ts +34 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +47 -0
- package/dist/constants.d.ts +10 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +9 -0
- package/dist/index.cjs +2108 -1379
- package/dist/index.d.mts +2440 -620
- package/dist/index.d.ts +17 -892
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.mjs +2093 -1362
- package/dist/plugins/api/index.d.ts +14 -0
- package/dist/plugins/api/index.d.ts.map +1 -0
- package/dist/plugins/api/index.js +21 -0
- package/dist/plugins/apps/index.d.ts +11 -0
- package/dist/plugins/apps/index.d.ts.map +1 -0
- package/dist/plugins/apps/index.js +91 -0
- package/dist/plugins/apps/types.d.ts +30 -0
- package/dist/plugins/apps/types.d.ts.map +1 -0
- package/dist/plugins/apps/types.js +2 -0
- package/dist/plugins/fetch/index.d.ts +21 -0
- package/dist/plugins/fetch/index.d.ts.map +1 -0
- package/dist/plugins/fetch/index.js +20 -0
- package/dist/plugins/findFirstAuthentication/index.d.ts +20 -0
- package/dist/plugins/findFirstAuthentication/index.d.ts.map +1 -0
- package/dist/plugins/findFirstAuthentication/index.js +24 -0
- package/dist/plugins/findFirstAuthentication/index.test.d.ts +2 -0
- package/dist/plugins/findFirstAuthentication/index.test.d.ts.map +1 -0
- package/dist/plugins/findFirstAuthentication/index.test.js +171 -0
- package/dist/plugins/findFirstAuthentication/schemas.d.ts +29 -0
- package/dist/plugins/findFirstAuthentication/schemas.d.ts.map +1 -0
- package/dist/plugins/findFirstAuthentication/schemas.js +18 -0
- package/dist/plugins/findUniqueAuthentication/index.d.ts +20 -0
- package/dist/plugins/findUniqueAuthentication/index.d.ts.map +1 -0
- package/dist/plugins/findUniqueAuthentication/index.js +31 -0
- package/dist/plugins/findUniqueAuthentication/index.test.d.ts +2 -0
- package/dist/plugins/findUniqueAuthentication/index.test.d.ts.map +1 -0
- package/dist/plugins/findUniqueAuthentication/index.test.js +152 -0
- package/dist/plugins/findUniqueAuthentication/schemas.d.ts +29 -0
- package/dist/plugins/findUniqueAuthentication/schemas.d.ts.map +1 -0
- package/dist/plugins/findUniqueAuthentication/schemas.js +18 -0
- package/dist/plugins/getAction/index.d.ts +23 -0
- package/dist/plugins/getAction/index.d.ts.map +1 -0
- package/dist/plugins/getAction/index.js +28 -0
- package/dist/plugins/getAction/index.test.d.ts +2 -0
- package/dist/plugins/getAction/index.test.d.ts.map +1 -0
- package/dist/plugins/getAction/index.test.js +186 -0
- package/dist/plugins/getAction/schemas.d.ts +23 -0
- package/dist/plugins/getAction/schemas.d.ts.map +1 -0
- package/dist/plugins/getAction/schemas.js +10 -0
- package/dist/plugins/getApp/index.d.ts +22 -0
- package/dist/plugins/getApp/index.d.ts.map +1 -0
- package/dist/plugins/getApp/index.js +39 -0
- package/dist/plugins/getApp/index.test.d.ts +2 -0
- package/dist/plugins/getApp/index.test.d.ts.map +1 -0
- package/dist/plugins/getApp/index.test.js +100 -0
- package/dist/plugins/getApp/schemas.d.ts +18 -0
- package/dist/plugins/getApp/schemas.d.ts.map +1 -0
- package/dist/plugins/getApp/schemas.js +10 -0
- package/dist/plugins/getAuthentication/index.d.ts +22 -0
- package/dist/plugins/getAuthentication/index.d.ts.map +1 -0
- package/dist/plugins/getAuthentication/index.js +41 -0
- package/dist/plugins/getAuthentication/index.test.d.ts +2 -0
- package/dist/plugins/getAuthentication/index.test.d.ts.map +1 -0
- package/dist/plugins/getAuthentication/index.test.js +205 -0
- package/dist/plugins/getAuthentication/schemas.d.ts +17 -0
- package/dist/plugins/getAuthentication/schemas.d.ts.map +1 -0
- package/dist/plugins/getAuthentication/schemas.js +11 -0
- package/dist/plugins/getProfile/index.d.ts +23 -0
- package/dist/plugins/getProfile/index.d.ts.map +1 -0
- package/dist/plugins/getProfile/index.js +29 -0
- package/dist/plugins/getProfile/schemas.d.ts +13 -0
- package/dist/plugins/getProfile/schemas.d.ts.map +1 -0
- package/dist/plugins/getProfile/schemas.js +5 -0
- package/dist/plugins/listActions/index.d.ts +28 -0
- package/dist/plugins/listActions/index.d.ts.map +1 -0
- package/dist/plugins/listActions/index.js +61 -0
- package/dist/plugins/listActions/index.test.d.ts +2 -0
- package/dist/plugins/listActions/index.test.d.ts.map +1 -0
- package/dist/plugins/listActions/index.test.js +467 -0
- package/dist/plugins/listActions/schemas.d.ts +29 -0
- package/dist/plugins/listActions/schemas.d.ts.map +1 -0
- package/dist/plugins/listActions/schemas.js +21 -0
- package/dist/plugins/listApps/index.d.ts +28 -0
- package/dist/plugins/listApps/index.d.ts.map +1 -0
- package/dist/plugins/listApps/index.js +62 -0
- package/dist/plugins/listApps/index.test.d.ts +2 -0
- package/dist/plugins/listApps/index.test.d.ts.map +1 -0
- package/dist/plugins/listApps/index.test.js +313 -0
- package/dist/plugins/listApps/schemas.d.ts +30 -0
- package/dist/plugins/listApps/schemas.d.ts.map +1 -0
- package/dist/plugins/listApps/schemas.js +23 -0
- package/dist/plugins/listAuthentications/index.d.ts +28 -0
- package/dist/plugins/listAuthentications/index.d.ts.map +1 -0
- package/dist/plugins/listAuthentications/index.js +77 -0
- package/dist/plugins/listAuthentications/index.test.d.ts +2 -0
- package/dist/plugins/listAuthentications/index.test.d.ts.map +1 -0
- package/dist/plugins/listAuthentications/index.test.js +564 -0
- package/dist/plugins/listAuthentications/schemas.d.ts +38 -0
- package/dist/plugins/listAuthentications/schemas.d.ts.map +1 -0
- package/dist/plugins/listAuthentications/schemas.js +28 -0
- package/dist/plugins/listInputFields/index.d.ts +28 -0
- package/dist/plugins/listInputFields/index.d.ts.map +1 -0
- package/dist/plugins/listInputFields/index.js +133 -0
- package/dist/plugins/listInputFields/index.test.d.ts +2 -0
- package/dist/plugins/listInputFields/index.test.d.ts.map +1 -0
- package/dist/plugins/listInputFields/index.test.js +325 -0
- package/dist/plugins/listInputFields/schemas.d.ts +38 -0
- package/dist/plugins/listInputFields/schemas.d.ts.map +1 -0
- package/dist/plugins/listInputFields/schemas.js +22 -0
- package/dist/plugins/registry/index.d.ts +11 -0
- package/dist/plugins/registry/index.d.ts.map +1 -0
- package/dist/plugins/registry/index.js +14 -0
- package/dist/plugins/request/index.d.ts +19 -0
- package/dist/plugins/request/index.d.ts.map +1 -0
- package/dist/plugins/request/index.js +62 -0
- package/dist/plugins/request/index.test.d.ts +2 -0
- package/dist/plugins/request/index.test.d.ts.map +1 -0
- package/dist/plugins/request/index.test.js +256 -0
- package/dist/plugins/request/schemas.d.ts +69 -0
- package/dist/plugins/request/schemas.d.ts.map +1 -0
- package/dist/plugins/request/schemas.js +42 -0
- package/dist/plugins/runAction/index.d.ts +28 -0
- package/dist/plugins/runAction/index.d.ts.map +1 -0
- package/dist/plugins/runAction/index.js +86 -0
- package/dist/plugins/runAction/index.test.d.ts +2 -0
- package/dist/plugins/runAction/index.test.d.ts.map +1 -0
- package/dist/plugins/runAction/index.test.js +320 -0
- package/dist/plugins/runAction/schemas.d.ts +37 -0
- package/dist/plugins/runAction/schemas.d.ts.map +1 -0
- package/dist/plugins/runAction/schemas.js +22 -0
- package/dist/resolvers/actionKey.d.ts +9 -0
- package/dist/resolvers/actionKey.d.ts.map +1 -0
- package/dist/resolvers/actionKey.js +19 -0
- package/dist/resolvers/actionType.d.ts +9 -0
- package/dist/resolvers/actionType.d.ts.map +1 -0
- package/dist/resolvers/actionType.js +22 -0
- package/dist/resolvers/appKey.d.ts +7 -0
- package/dist/resolvers/appKey.d.ts.map +1 -0
- package/dist/resolvers/appKey.js +5 -0
- package/dist/resolvers/authenticationId.d.ts +9 -0
- package/dist/resolvers/authenticationId.d.ts.map +1 -0
- package/dist/resolvers/authenticationId.js +33 -0
- package/dist/resolvers/index.d.ts +40 -0
- package/dist/resolvers/index.d.ts.map +1 -0
- package/dist/resolvers/index.js +91 -0
- package/dist/resolvers/inputs.d.ts +8 -0
- package/dist/resolvers/inputs.d.ts.map +1 -0
- package/dist/resolvers/inputs.js +14 -0
- package/dist/schemas/Action.d.ts +243 -0
- package/dist/schemas/Action.d.ts.map +1 -0
- package/dist/schemas/Action.js +34 -0
- package/dist/schemas/App.d.ts +26 -0
- package/dist/schemas/App.d.ts.map +1 -0
- package/dist/schemas/App.js +22 -0
- package/dist/schemas/Auth.d.ts +161 -0
- package/dist/schemas/Auth.d.ts.map +1 -0
- package/dist/schemas/Auth.js +41 -0
- package/dist/schemas/Field.d.ts +144 -0
- package/dist/schemas/Field.d.ts.map +1 -0
- package/dist/schemas/Field.js +105 -0
- package/dist/schemas/UserProfile.d.ts +163 -0
- package/dist/schemas/UserProfile.d.ts.map +1 -0
- package/dist/schemas/UserProfile.js +29 -0
- package/dist/sdk.d.ts +10 -0
- package/dist/sdk.d.ts.map +1 -0
- package/dist/sdk.js +94 -0
- package/dist/sdk.test.d.ts +2 -0
- package/dist/sdk.test.d.ts.map +1 -0
- package/dist/sdk.test.js +135 -0
- package/dist/types/domain.d.ts +36 -0
- package/dist/types/domain.d.ts.map +1 -0
- package/dist/types/domain.js +1 -0
- package/dist/types/domain.test.d.ts +2 -0
- package/dist/types/domain.test.d.ts.map +1 -0
- package/dist/types/domain.test.js +39 -0
- package/dist/types/errors.d.ts +143 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/errors.js +187 -0
- package/dist/types/events.d.ts +38 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +7 -0
- package/dist/types/functions.d.ts +26 -0
- package/dist/types/functions.d.ts.map +1 -0
- package/dist/types/functions.js +4 -0
- package/dist/types/plugin.d.ts +61 -0
- package/dist/types/plugin.d.ts.map +1 -0
- package/dist/types/plugin.js +9 -0
- package/dist/types/properties.d.ts +22 -0
- package/dist/types/properties.d.ts.map +1 -0
- package/dist/types/properties.js +50 -0
- package/dist/types/sdk.d.ts +43 -0
- package/dist/types/sdk.d.ts.map +1 -0
- package/dist/types/sdk.js +4 -0
- package/dist/utils/array-utils.d.ts +31 -0
- package/dist/utils/array-utils.d.ts.map +1 -0
- package/dist/utils/array-utils.js +36 -0
- package/dist/utils/array-utils.test.d.ts +2 -0
- package/dist/utils/array-utils.test.d.ts.map +1 -0
- package/dist/utils/array-utils.test.js +107 -0
- package/dist/utils/domain-utils.d.ts +78 -0
- package/dist/utils/domain-utils.d.ts.map +1 -0
- package/dist/utils/domain-utils.js +218 -0
- package/dist/utils/domain-utils.test.d.ts +2 -0
- package/dist/utils/domain-utils.test.d.ts.map +1 -0
- package/dist/utils/domain-utils.test.js +192 -0
- package/dist/utils/function-utils.d.ts +45 -0
- package/dist/utils/function-utils.d.ts.map +1 -0
- package/dist/utils/function-utils.js +158 -0
- package/dist/utils/function-utils.test.d.ts +2 -0
- package/dist/utils/function-utils.test.d.ts.map +1 -0
- package/dist/utils/function-utils.test.js +110 -0
- package/dist/utils/pagination-utils.d.ts +37 -0
- package/dist/utils/pagination-utils.d.ts.map +1 -0
- package/dist/utils/pagination-utils.js +165 -0
- package/dist/utils/pagination-utils.test.d.ts +17 -0
- package/dist/utils/pagination-utils.test.d.ts.map +1 -0
- package/dist/utils/pagination-utils.test.js +461 -0
- package/dist/utils/schema-utils.d.ts +45 -0
- package/dist/utils/schema-utils.d.ts.map +1 -0
- package/dist/utils/schema-utils.js +65 -0
- package/dist/utils/validation.d.ts +4 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +30 -0
- package/dist/utils/validation.test.d.ts +2 -0
- package/dist/utils/validation.test.d.ts.map +1 -0
- package/dist/utils/validation.test.js +43 -0
- package/package.json +12 -3
- package/src/api/client.ts +394 -171
- package/src/api/debug.ts +10 -1
- package/src/api/index.ts +0 -2
- package/src/api/polling.ts +28 -7
- package/src/api/schemas.ts +387 -0
- package/src/api/types.ts +72 -136
- package/src/constants.ts +10 -0
- package/src/index.ts +40 -19
- package/src/plugins/api/index.ts +47 -0
- package/src/plugins/apps/index.ts +25 -19
- package/src/plugins/apps/types.ts +7 -11
- package/src/plugins/fetch/index.ts +48 -40
- package/src/plugins/findFirstAuthentication/index.test.ts +206 -0
- package/src/plugins/findFirstAuthentication/index.ts +55 -0
- package/src/plugins/findFirstAuthentication/schemas.ts +41 -0
- package/src/plugins/findUniqueAuthentication/index.test.ts +197 -0
- package/src/plugins/findUniqueAuthentication/index.ts +72 -0
- package/src/plugins/findUniqueAuthentication/schemas.ts +42 -0
- package/src/plugins/getAction/index.test.ts +239 -0
- package/src/plugins/getAction/index.ts +57 -0
- package/src/plugins/getAction/schemas.ts +33 -0
- package/src/plugins/getApp/index.test.ts +127 -0
- package/src/plugins/getApp/index.ts +66 -0
- package/src/plugins/getApp/schemas.ts +38 -0
- package/src/plugins/getAuthentication/index.test.ts +284 -0
- package/src/plugins/getAuthentication/index.ts +86 -0
- package/src/plugins/getAuthentication/schemas.ts +31 -0
- package/src/plugins/getProfile/index.ts +55 -0
- package/src/plugins/getProfile/schemas.ts +26 -0
- package/src/plugins/listActions/index.test.ts +582 -0
- package/src/plugins/listActions/index.ts +115 -0
- package/src/plugins/listActions/schemas.ts +54 -0
- package/src/plugins/listApps/index.test.ts +357 -0
- package/src/plugins/listApps/index.ts +121 -0
- package/src/plugins/listApps/schemas.ts +49 -0
- package/src/plugins/listAuthentications/index.test.ts +709 -0
- package/src/plugins/listAuthentications/index.ts +136 -0
- package/src/plugins/listAuthentications/schemas.ts +60 -0
- package/src/plugins/listInputFields/index.test.ts +408 -0
- package/src/plugins/listInputFields/index.ts +204 -0
- package/src/plugins/listInputFields/schemas.ts +56 -0
- package/src/plugins/registry/index.ts +30 -0
- package/src/plugins/request/index.test.ts +329 -0
- package/src/plugins/request/index.ts +103 -0
- package/src/{functions → plugins}/request/schemas.ts +20 -9
- package/src/plugins/runAction/index.test.ts +387 -0
- package/src/plugins/runAction/index.ts +176 -0
- package/src/plugins/runAction/schemas.ts +53 -0
- package/src/resolvers/actionKey.ts +6 -4
- package/src/resolvers/actionType.ts +7 -2
- package/src/resolvers/appKey.ts +1 -1
- package/src/resolvers/authenticationId.ts +12 -3
- package/src/resolvers/inputs.ts +3 -1
- package/src/schemas/Action.ts +18 -12
- package/src/schemas/App.ts +11 -19
- package/src/schemas/Auth.ts +18 -13
- package/src/schemas/Field.ts +106 -11
- package/src/schemas/UserProfile.ts +43 -0
- package/src/sdk.test.ts +212 -0
- package/src/sdk.ts +132 -102
- package/src/types/domain.test.ts +50 -0
- package/src/types/domain.ts +43 -75
- package/src/types/errors.ts +275 -0
- package/src/types/functions.ts +27 -0
- package/src/types/optional-zapier-sdk-cli-login.d.ts +37 -0
- package/src/types/plugin.ts +105 -0
- package/src/types/properties.ts +4 -3
- package/src/types/sdk.ts +70 -48
- package/src/utils/array-utils.test.ts +131 -0
- package/src/utils/array-utils.ts +41 -0
- package/src/utils/domain-utils.test.ts +239 -0
- package/src/utils/domain-utils.ts +283 -0
- package/src/utils/function-utils.test.ts +141 -0
- package/src/utils/function-utils.ts +245 -0
- package/src/utils/pagination-utils.test.ts +620 -0
- package/src/utils/pagination-utils.ts +242 -0
- package/src/utils/validation.test.ts +50 -0
- package/src/utils/validation.ts +44 -0
- package/tsconfig.build.json +16 -2
- package/tsconfig.json +3 -1
- package/tsconfig.tsbuildinfo +1 -0
- package/tsup.config.ts +2 -0
- package/src/functions/bundleCode/index.ts +0 -78
- package/src/functions/bundleCode/info.ts +0 -9
- package/src/functions/bundleCode/schemas.ts +0 -30
- package/src/functions/findFirstAuthentication/index.ts +0 -24
- package/src/functions/findFirstAuthentication/info.ts +0 -9
- package/src/functions/findFirstAuthentication/schemas.ts +0 -50
- package/src/functions/findUniqueAuthentication/index.ts +0 -35
- package/src/functions/findUniqueAuthentication/info.ts +0 -9
- package/src/functions/findUniqueAuthentication/schemas.ts +0 -50
- package/src/functions/generateTypes/index.ts +0 -363
- package/src/functions/generateTypes/info.ts +0 -9
- package/src/functions/generateTypes/schemas.ts +0 -31
- package/src/functions/getAction/index.ts +0 -33
- package/src/functions/getAction/info.ts +0 -9
- package/src/functions/getAction/schemas.ts +0 -25
- package/src/functions/getApp/index.ts +0 -41
- package/src/functions/getApp/info.ts +0 -9
- package/src/functions/getApp/schemas.ts +0 -20
- package/src/functions/getAuthentication/index.ts +0 -50
- package/src/functions/getAuthentication/info.ts +0 -9
- package/src/functions/getAuthentication/schemas.ts +0 -29
- package/src/functions/listActions/index.ts +0 -149
- package/src/functions/listActions/info.ts +0 -9
- package/src/functions/listActions/schemas.ts +0 -30
- package/src/functions/listApps/index.ts +0 -60
- package/src/functions/listApps/info.ts +0 -9
- package/src/functions/listApps/schemas.ts +0 -32
- package/src/functions/listAuthentications/index.ts +0 -162
- package/src/functions/listAuthentications/info.ts +0 -9
- package/src/functions/listAuthentications/schemas.ts +0 -50
- package/src/functions/listFields/index.ts +0 -86
- package/src/functions/listFields/info.ts +0 -9
- package/src/functions/listFields/schemas.ts +0 -36
- package/src/functions/request/index.ts +0 -150
- package/src/functions/request/info.ts +0 -11
- package/src/functions/runAction/index.ts +0 -127
- package/src/functions/runAction/info.ts +0 -9
- package/src/functions/runAction/schemas.ts +0 -34
- package/src/plugins/apps/info.ts +0 -12
- package/src/plugins/fetch/types.ts +0 -2
- /package/src/{schema-utils.ts → utils/schema-utils.ts} +0 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Domain utility functions for working with app identifiers and data normalization
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Splits a versioned key to extract the base key and version
|
|
6
|
+
*
|
|
7
|
+
* @param versionedKey - Versioned key in format "KeyName@version" (e.g., "SlackCLIAPI@1.21.1")
|
|
8
|
+
* @returns Tuple of [baseKey, version] or [versionedKey, undefined] if no @ symbol
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* splitVersionedKey("SlackCLIAPI@1.21.1") // ["SlackCLIAPI", "1.21.1"]
|
|
12
|
+
* splitVersionedKey("SomeAPI") // ["SomeAPI", undefined]
|
|
13
|
+
*/
|
|
14
|
+
export function splitVersionedKey(versionedKey) {
|
|
15
|
+
const parts = versionedKey.split("@");
|
|
16
|
+
if (parts.length >= 2) {
|
|
17
|
+
const baseKey = parts[0];
|
|
18
|
+
const version = parts.slice(1).join("@"); // Handle edge case of multiple @ symbols
|
|
19
|
+
return [baseKey, version];
|
|
20
|
+
}
|
|
21
|
+
return [versionedKey, undefined];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Converts an API App to an AppItem with normalized field names
|
|
25
|
+
*
|
|
26
|
+
* @param app - Raw App from API
|
|
27
|
+
* @returns Normalized AppItem
|
|
28
|
+
*/
|
|
29
|
+
export function normalizeAppItem(app) {
|
|
30
|
+
// Extract API name and version from current_implementation_id
|
|
31
|
+
const [apiName, appVersion] = app.current_implementation_id
|
|
32
|
+
? splitVersionedKey(app.current_implementation_id)
|
|
33
|
+
: [app.current_implementation_id || "", undefined];
|
|
34
|
+
return {
|
|
35
|
+
title: app.name,
|
|
36
|
+
key: app.slug || apiName,
|
|
37
|
+
current_implementation_id: app.current_implementation_id || "",
|
|
38
|
+
version: appVersion, // Extract version separately
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Converts an Implementation to an AppItem with normalized field names
|
|
43
|
+
*
|
|
44
|
+
* @param implementation - Raw Implementation from API
|
|
45
|
+
* @returns Normalized AppItem
|
|
46
|
+
*/
|
|
47
|
+
export function normalizeImplementationToAppItem(implementation) {
|
|
48
|
+
// Extract API name and version from selected_api
|
|
49
|
+
const [selectedApi, appVersion] = implementation.selected_api
|
|
50
|
+
? splitVersionedKey(implementation.selected_api)
|
|
51
|
+
: [implementation.selected_api || "", undefined];
|
|
52
|
+
return {
|
|
53
|
+
title: implementation.name || selectedApi,
|
|
54
|
+
key: selectedApi,
|
|
55
|
+
current_implementation_id: implementation.selected_api || "",
|
|
56
|
+
version: appVersion, // Extract version separately
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Converts a lightweight ImplementationMeta to a slimmed AppItem
|
|
61
|
+
*
|
|
62
|
+
* @param implementationMeta - Raw ImplementationMeta from API
|
|
63
|
+
* @returns Normalized AppItem with essential fields only
|
|
64
|
+
*/
|
|
65
|
+
export function normalizeImplementationMetaToAppItem(implementationMeta) {
|
|
66
|
+
// Extract API name and version from the implementation ID
|
|
67
|
+
const [selectedApi, appVersion] = splitVersionedKey(implementationMeta.id);
|
|
68
|
+
return {
|
|
69
|
+
title: implementationMeta.name,
|
|
70
|
+
key: selectedApi,
|
|
71
|
+
current_implementation_id: implementationMeta.id, // Keep the full versioned ID
|
|
72
|
+
version: appVersion, // Extract version separately
|
|
73
|
+
slug: implementationMeta.slug,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
export function normalizeServiceToAppItem(service) {
|
|
77
|
+
const [selectedApi, appVersion] = service.current_implementation_id
|
|
78
|
+
? splitVersionedKey(service.current_implementation_id)
|
|
79
|
+
: ["", undefined];
|
|
80
|
+
return {
|
|
81
|
+
title: service.name,
|
|
82
|
+
key: selectedApi,
|
|
83
|
+
current_implementation_id: service.current_implementation_id,
|
|
84
|
+
version: appVersion,
|
|
85
|
+
description: service.description,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Converts an API Authentication to an AuthenticationItem with normalized field names
|
|
90
|
+
*
|
|
91
|
+
* @param auth - Raw Authentication from API
|
|
92
|
+
* @param options - Additional fields to include
|
|
93
|
+
* @param options.app_key - selected_api from implementations endpoint
|
|
94
|
+
* @param options.version - Version extracted from selected_api
|
|
95
|
+
* @returns Normalized AuthenticationItem
|
|
96
|
+
*/
|
|
97
|
+
export function normalizeAuthenticationItem(auth, options = {}) {
|
|
98
|
+
// Extract app key and version from selected_api if not provided
|
|
99
|
+
let appKey = options.app_key;
|
|
100
|
+
let version = options.version;
|
|
101
|
+
if (auth.selected_api) {
|
|
102
|
+
const [extractedAppKey, extractedVersion] = splitVersionedKey(auth.selected_api);
|
|
103
|
+
// Use extracted app key if not provided in options
|
|
104
|
+
if (!appKey) {
|
|
105
|
+
appKey = extractedAppKey;
|
|
106
|
+
}
|
|
107
|
+
// Use extracted version if not provided in options
|
|
108
|
+
if (!version) {
|
|
109
|
+
version = extractedVersion;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const { selected_api: selectedApi, customuser_id: userId, ...restOfAuth } = auth;
|
|
113
|
+
return {
|
|
114
|
+
...restOfAuth, // Pass through all other API response fields except selected_api
|
|
115
|
+
implementation_id: selectedApi, // Rename selected_api to implementation_id
|
|
116
|
+
title: auth.title || auth.label || undefined, // Coerce title from label if missing
|
|
117
|
+
is_expired: auth.is_stale, // Map is_stale to is_expired
|
|
118
|
+
expired_at: auth.marked_stale_at, // Map marked_stale_at to expired_at
|
|
119
|
+
app_key: appKey, // App key from implementations endpoint or parsed from selected_api
|
|
120
|
+
version, // Version from selected_api or provided
|
|
121
|
+
user_id: userId, // Map customuser_id to user_id
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
export function normalizeActionItem(action) {
|
|
125
|
+
const { name, type, selected_api: appKey, ...restOfAction } = action;
|
|
126
|
+
return {
|
|
127
|
+
...restOfAction,
|
|
128
|
+
app_key: appKey || "",
|
|
129
|
+
action_type: type,
|
|
130
|
+
title: name, // Map name to title
|
|
131
|
+
type: "action",
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Groups app keys by their type based on format patterns
|
|
136
|
+
*
|
|
137
|
+
* @param appKeys Array of app key strings to categorize
|
|
138
|
+
* @returns Object with arrays of keys grouped by type: selectedApi (CLI APIs), slug (everything else)
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* groupVersionedAppKeysByType([
|
|
142
|
+
* 'FormatterCLIAPI@1.0.0',
|
|
143
|
+
* 'slack',
|
|
144
|
+
* 'AnotherAPI'
|
|
145
|
+
* ])
|
|
146
|
+
* // Returns: {
|
|
147
|
+
* // selectedApi: ['FormatterCLIAPI@1.0.0', 'AnotherAPI'],
|
|
148
|
+
* // slug: ['slack']
|
|
149
|
+
* // }
|
|
150
|
+
*/
|
|
151
|
+
export function groupVersionedAppKeysByType(appKeys) {
|
|
152
|
+
const result = {
|
|
153
|
+
selectedApi: [],
|
|
154
|
+
slug: [],
|
|
155
|
+
};
|
|
156
|
+
// Use Sets to track seen keys for deduplication
|
|
157
|
+
const seenSelectedApi = new Set();
|
|
158
|
+
const seenSlugs = new Set();
|
|
159
|
+
for (const key of appKeys) {
|
|
160
|
+
// Split by @ to get base name for classification (but preserve full key in results)
|
|
161
|
+
const [keyWithoutVersion, version] = splitVersionedKey(key);
|
|
162
|
+
// Check if it's a UUID (canonical ID) - not supported
|
|
163
|
+
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
164
|
+
if (uuidRegex.test(keyWithoutVersion)) {
|
|
165
|
+
throw new Error(`UUID app keys are not supported. Use app slug or implementation ID instead of: ${key}`);
|
|
166
|
+
}
|
|
167
|
+
// Check if it's a snake_case string
|
|
168
|
+
if (isSnakeCasedSlug(keyWithoutVersion)) {
|
|
169
|
+
const dashified = dashifySnakeCasedSlug(keyWithoutVersion);
|
|
170
|
+
const slugWithVersion = version ? `${dashified}@${version}` : dashified;
|
|
171
|
+
if (!seenSlugs.has(slugWithVersion)) {
|
|
172
|
+
seenSlugs.add(slugWithVersion);
|
|
173
|
+
result.slug.push(slugWithVersion); // Preserve full key including version
|
|
174
|
+
}
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
// Check if it's a slug (lowercase and dashes)
|
|
178
|
+
if (keyWithoutVersion.match(/^[a-z0-9]+(?:-[a-z0-9]+)*$/)) {
|
|
179
|
+
seenSlugs.add(key);
|
|
180
|
+
result.slug.push(key);
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
// Everything else is a selected_api
|
|
184
|
+
if (!seenSelectedApi.has(key)) {
|
|
185
|
+
seenSelectedApi.add(key);
|
|
186
|
+
result.selectedApi.push(key); // Preserve full key including version
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
export function groupAppKeysByType(appKeys) {
|
|
192
|
+
const grouped = groupVersionedAppKeysByType(appKeys);
|
|
193
|
+
// Extract base keys and dedupe by using Set
|
|
194
|
+
return {
|
|
195
|
+
selectedApi: [
|
|
196
|
+
...new Set(grouped.selectedApi.map((key) => key.split("@")[0])),
|
|
197
|
+
],
|
|
198
|
+
slug: [...new Set(grouped.slug.map((key) => key.split("@")[0]))],
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function isSnakeCasedSlug(slug) {
|
|
202
|
+
// Allow leading underscore for slugs starting with a number.
|
|
203
|
+
if (slug.match(/^_[0-9]/)) {
|
|
204
|
+
slug = slug.slice(1);
|
|
205
|
+
}
|
|
206
|
+
return !!slug.match(/^[a-z0-9]+(?:_[a-z0-9]+)*$/);
|
|
207
|
+
}
|
|
208
|
+
function dashifySnakeCasedSlug(slug) {
|
|
209
|
+
// Only dashify if it's a valid snake_cased slug.
|
|
210
|
+
if (!isSnakeCasedSlug(slug)) {
|
|
211
|
+
return slug;
|
|
212
|
+
}
|
|
213
|
+
// Allow one leading underscore for slugs starting with a number.
|
|
214
|
+
if (slug.startsWith("_")) {
|
|
215
|
+
slug = slug.slice(1);
|
|
216
|
+
}
|
|
217
|
+
return slug.replace(/_/g, "-");
|
|
218
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"domain-utils.test.d.ts","sourceRoot":"","sources":["../../src/utils/domain-utils.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { groupVersionedAppKeysByType, groupAppKeysByType, } from "./domain-utils";
|
|
3
|
+
describe("domain-utils", () => {
|
|
4
|
+
describe("groupVersionedAppKeysByType", () => {
|
|
5
|
+
it("should reject UUID app keys", () => {
|
|
6
|
+
expect(() => {
|
|
7
|
+
groupVersionedAppKeysByType(["61e47557-af91-4b0c-a3e0-c28606357664"]);
|
|
8
|
+
}).toThrow("UUID app keys are not supported. Use app slug or implementation ID instead of: 61e47557-af91-4b0c-a3e0-c28606357664");
|
|
9
|
+
});
|
|
10
|
+
it("should categorize *API as selectedApi", () => {
|
|
11
|
+
const result = groupVersionedAppKeysByType(["FormatterCLIAPI"]);
|
|
12
|
+
expect(result).toEqual({
|
|
13
|
+
selectedApi: ["FormatterCLIAPI"],
|
|
14
|
+
slug: [],
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
it("should categorize various API types as selectedApi", () => {
|
|
18
|
+
const result = groupVersionedAppKeysByType([
|
|
19
|
+
"FormatterCLIAPI",
|
|
20
|
+
"SlackAPI",
|
|
21
|
+
"GmailRESTAPI",
|
|
22
|
+
"WebhookAPI",
|
|
23
|
+
"AnythingWithUppercase",
|
|
24
|
+
]);
|
|
25
|
+
expect(result).toEqual({
|
|
26
|
+
selectedApi: [
|
|
27
|
+
"FormatterCLIAPI",
|
|
28
|
+
"SlackAPI",
|
|
29
|
+
"GmailRESTAPI",
|
|
30
|
+
"WebhookAPI",
|
|
31
|
+
"AnythingWithUppercase",
|
|
32
|
+
],
|
|
33
|
+
slug: [],
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
it("should categorize lower-case strings as slugs, converting underscores to dashes", () => {
|
|
37
|
+
const result = groupVersionedAppKeysByType([
|
|
38
|
+
"slack",
|
|
39
|
+
"google-sheets",
|
|
40
|
+
"_100hires_ats",
|
|
41
|
+
"amazon_redshift",
|
|
42
|
+
]);
|
|
43
|
+
expect(result).toEqual({
|
|
44
|
+
selectedApi: [],
|
|
45
|
+
slug: ["slack", "google-sheets", "100hires-ats", "amazon-redshift"],
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
it("should preserve version from API implementations", () => {
|
|
49
|
+
const result = groupVersionedAppKeysByType(["FormatterCLIAPI@1.0.0"]);
|
|
50
|
+
expect(result).toEqual({
|
|
51
|
+
selectedApi: ["FormatterCLIAPI@1.0.0"],
|
|
52
|
+
slug: [],
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
it("should preserve version from slugs", () => {
|
|
56
|
+
const result = groupVersionedAppKeysByType(["slack@2.1.0"]);
|
|
57
|
+
expect(result).toEqual({
|
|
58
|
+
selectedApi: [],
|
|
59
|
+
slug: ["slack@2.1.0"],
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
it("should handle mixed types correctly", () => {
|
|
63
|
+
const result = groupVersionedAppKeysByType([
|
|
64
|
+
"FormatterCLIAPI@1.0.0",
|
|
65
|
+
"slack",
|
|
66
|
+
"AnotherAPI",
|
|
67
|
+
"gmail@3.0.1",
|
|
68
|
+
]);
|
|
69
|
+
expect(result).toEqual({
|
|
70
|
+
selectedApi: ["FormatterCLIAPI@1.0.0", "AnotherAPI"],
|
|
71
|
+
slug: ["slack", "gmail@3.0.1"],
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
it("should handle empty array", () => {
|
|
75
|
+
const result = groupVersionedAppKeysByType([]);
|
|
76
|
+
expect(result).toEqual({
|
|
77
|
+
selectedApi: [],
|
|
78
|
+
slug: [],
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
it("should reject uppercase and lowercase UUIDs", () => {
|
|
82
|
+
expect(() => {
|
|
83
|
+
groupVersionedAppKeysByType(["61E47557-AF91-4B0C-A3E0-C28606357664"]);
|
|
84
|
+
}).toThrow("UUID app keys are not supported. Use app slug or implementation ID instead of: 61E47557-AF91-4B0C-A3E0-C28606357664");
|
|
85
|
+
expect(() => {
|
|
86
|
+
groupVersionedAppKeysByType(["f47ac10b-58cc-4372-a567-0e02b2c3d479"]);
|
|
87
|
+
}).toThrow("UUID app keys are not supported. Use app slug or implementation ID instead of: f47ac10b-58cc-4372-a567-0e02b2c3d479");
|
|
88
|
+
});
|
|
89
|
+
it("should categorize partial UUIDs as slugs", () => {
|
|
90
|
+
const result = groupVersionedAppKeysByType([
|
|
91
|
+
"61e47557-af91-4b0c-a3e0", // too short
|
|
92
|
+
"61e47557-af91-4b0c-a3e0-c28606357664-extra", // too long
|
|
93
|
+
]);
|
|
94
|
+
expect(result).toEqual({
|
|
95
|
+
selectedApi: [],
|
|
96
|
+
slug: [
|
|
97
|
+
"61e47557-af91-4b0c-a3e0",
|
|
98
|
+
"61e47557-af91-4b0c-a3e0-c28606357664-extra",
|
|
99
|
+
],
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
it("should dedupe exact duplicate versioned keys", () => {
|
|
103
|
+
const result = groupVersionedAppKeysByType([
|
|
104
|
+
"slack@2.1.0",
|
|
105
|
+
"FormatterCLIAPI@1.0.0",
|
|
106
|
+
"slack@2.1.0", // exact duplicate
|
|
107
|
+
"FormatterCLIAPI@1.0.0", // exact duplicate
|
|
108
|
+
]);
|
|
109
|
+
expect(result).toEqual({
|
|
110
|
+
selectedApi: ["FormatterCLIAPI@1.0.0"],
|
|
111
|
+
slug: ["slack@2.1.0"],
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
describe("groupAppKeysByType", () => {
|
|
116
|
+
it("should strip versions from API implementations", () => {
|
|
117
|
+
const result = groupAppKeysByType(["FormatterCLIAPI@1.0.0"]);
|
|
118
|
+
expect(result).toEqual({
|
|
119
|
+
selectedApi: ["FormatterCLIAPI"],
|
|
120
|
+
slug: [],
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
it("should strip versions from slugs", () => {
|
|
124
|
+
const result = groupAppKeysByType(["slack@2.1.0"]);
|
|
125
|
+
expect(result).toEqual({
|
|
126
|
+
selectedApi: [],
|
|
127
|
+
slug: ["slack"],
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
it("should handle mixed types and strip versions", () => {
|
|
131
|
+
const result = groupAppKeysByType([
|
|
132
|
+
"FormatterCLIAPI@1.0.0",
|
|
133
|
+
"slack",
|
|
134
|
+
"AnotherAPI@2.3.4",
|
|
135
|
+
"gmail@3.0.1",
|
|
136
|
+
]);
|
|
137
|
+
expect(result).toEqual({
|
|
138
|
+
selectedApi: ["FormatterCLIAPI", "AnotherAPI"],
|
|
139
|
+
slug: ["slack", "gmail"],
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
it("should handle keys without versions", () => {
|
|
143
|
+
const result = groupAppKeysByType([
|
|
144
|
+
"FormatterCLIAPI",
|
|
145
|
+
"slack",
|
|
146
|
+
"AnotherAPI",
|
|
147
|
+
]);
|
|
148
|
+
expect(result).toEqual({
|
|
149
|
+
selectedApi: ["FormatterCLIAPI", "AnotherAPI"],
|
|
150
|
+
slug: ["slack"],
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
it("should reject UUID app keys (delegated to versioned function)", () => {
|
|
154
|
+
expect(() => {
|
|
155
|
+
groupAppKeysByType(["61e47557-af91-4b0c-a3e0-c28606357664"]);
|
|
156
|
+
}).toThrow("UUID app keys are not supported. Use app slug or implementation ID instead of: 61e47557-af91-4b0c-a3e0-c28606357664");
|
|
157
|
+
});
|
|
158
|
+
it("should handle empty array", () => {
|
|
159
|
+
const result = groupAppKeysByType([]);
|
|
160
|
+
expect(result).toEqual({
|
|
161
|
+
selectedApi: [],
|
|
162
|
+
slug: [],
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
it("should dedupe multiple versions of same base key", () => {
|
|
166
|
+
const result = groupAppKeysByType([
|
|
167
|
+
"slack@1.0.0",
|
|
168
|
+
"FormatterCLIAPI@2.1.0",
|
|
169
|
+
"slack@1.2.3", // different version of slack
|
|
170
|
+
"FormatterCLIAPI@1.0.0", // different version of FormatterCLIAPI
|
|
171
|
+
"gmail",
|
|
172
|
+
]);
|
|
173
|
+
expect(result).toEqual({
|
|
174
|
+
selectedApi: ["FormatterCLIAPI"], // only one instance despite multiple versions
|
|
175
|
+
slug: ["slack", "gmail"], // only one slack despite multiple versions
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
it("should dedupe exact duplicates after version stripping", () => {
|
|
179
|
+
const result = groupAppKeysByType([
|
|
180
|
+
"slack@1.0.0",
|
|
181
|
+
"slack", // no version
|
|
182
|
+
"slack@2.0.0", // different version
|
|
183
|
+
"FormatterCLIAPI",
|
|
184
|
+
"FormatterCLIAPI@1.5.0",
|
|
185
|
+
]);
|
|
186
|
+
expect(result).toEqual({
|
|
187
|
+
selectedApi: ["FormatterCLIAPI"],
|
|
188
|
+
slug: ["slack"],
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic utility functions for creating paginated SDK functions
|
|
3
|
+
*/
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
/**
|
|
6
|
+
* Helper function to extract cursor from pagination data
|
|
7
|
+
*/
|
|
8
|
+
export declare function extractCursor(data: {
|
|
9
|
+
next?: string | null;
|
|
10
|
+
} | null | undefined): string | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* Higher-order function that creates a function from a core function
|
|
13
|
+
*
|
|
14
|
+
* @param coreFn - Function that returns T directly or throws errors
|
|
15
|
+
* @param schema - Optional Zod schema for validation
|
|
16
|
+
* @returns A function that normalizes and rethrows errors
|
|
17
|
+
*/
|
|
18
|
+
export declare function createFunction<TOptions, TResult, TSchemaOptions extends TOptions = TOptions>(coreFn: (options: TOptions) => Promise<TResult>, schema?: z.ZodSchema<TSchemaOptions>): (options?: TOptions) => Promise<TResult>;
|
|
19
|
+
/**
|
|
20
|
+
* Higher-order function that creates a paginated function that wraps results in {data}
|
|
21
|
+
*
|
|
22
|
+
* @param coreFn - Function that returns T directly or throws errors
|
|
23
|
+
* @returns A function that normalizes errors and wraps results in {data}
|
|
24
|
+
*/
|
|
25
|
+
type PaginatedResult<TResult> = TResult extends {
|
|
26
|
+
data: any;
|
|
27
|
+
nextCursor?: string;
|
|
28
|
+
} ? TResult : {
|
|
29
|
+
data: TResult;
|
|
30
|
+
nextCursor?: string;
|
|
31
|
+
};
|
|
32
|
+
type ItemType<TResult> = TResult extends {
|
|
33
|
+
data: infer TData;
|
|
34
|
+
} ? TData extends readonly (infer TItem)[] ? TItem : TData : TResult extends readonly (infer TItem)[] ? TItem : TResult;
|
|
35
|
+
export declare function createPaginatedFunction<TOptions, TResult, TSchemaOptions extends TOptions = TOptions>(coreFn: (options: TOptions & {
|
|
36
|
+
pageSize: number;
|
|
37
|
+
}) => Promise<TResult>, schema?: z.ZodSchema<TSchemaOptions>): (options?: TOptions & {
|
|
38
|
+
cursor?: string;
|
|
39
|
+
pageSize?: number;
|
|
40
|
+
maxItems?: number;
|
|
41
|
+
}) => Promise<PaginatedResult<TResult>> & AsyncIterable<PaginatedResult<TResult>> & {
|
|
42
|
+
items(): AsyncIterable<ItemType<TResult>>;
|
|
43
|
+
};
|
|
44
|
+
export {};
|
|
45
|
+
//# sourceMappingURL=function-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-utils.d.ts","sourceRoot":"","sources":["../../src/utils/function-utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAKxB;;GAEG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,GAAG,IAAI,GAAG,SAAS,GAChD,MAAM,GAAG,SAAS,CAYpB;AAcD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,QAAQ,EACR,OAAO,EACP,cAAc,SAAS,QAAQ,GAAG,QAAQ,EAE1C,MAAM,EAAE,CAAC,OAAO,EAAE,QAAQ,KAAK,OAAO,CAAC,OAAO,CAAC,EAC/C,MAAM,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,cAAc,CAAC,cAMQ,QAAQ,KAAG,OAAO,CAAC,OAAO,CAAC,CAqBxE;AAiDD;;;;;GAKG;AACH,KAAK,eAAe,CAAC,OAAO,IAAI,OAAO,SAAS;IAC9C,IAAI,EAAE,GAAG,CAAC;IACV,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,GACG,OAAO,GACP;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,UAAU,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAE3C,KAAK,QAAQ,CAAC,OAAO,IAAI,OAAO,SAAS;IAAE,IAAI,EAAE,MAAM,KAAK,CAAA;CAAE,GAC1D,KAAK,SAAS,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,GACpC,KAAK,GACL,KAAK,GACP,OAAO,SAAS,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,GACtC,KAAK,GACL,OAAO,CAAC;AAEd,wBAAgB,uBAAuB,CACrC,QAAQ,EACR,OAAO,EACP,cAAc,SAAS,QAAQ,GAAG,QAAQ,EAE1C,MAAM,EAAE,CAAC,OAAO,EAAE,QAAQ,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,KAAK,OAAO,CAAC,OAAO,CAAC,EACtE,MAAM,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,cAAc,CAAC,GACnC,CACD,OAAO,CAAC,EAAE,QAAQ,GAAG;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,KACE,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,GACpC,aAAa,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,GAAG;IACxC,KAAK,IAAI,aAAa,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC;CAC3C,CA8EF"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic utility functions for creating paginated SDK functions
|
|
3
|
+
*/
|
|
4
|
+
import { ZapierError, ZapierUnknownError } from "../types/errors";
|
|
5
|
+
import { paginate } from "./pagination-utils";
|
|
6
|
+
import { createValidator, validateOptions } from "./validation";
|
|
7
|
+
/**
|
|
8
|
+
* Helper function to extract cursor from pagination data
|
|
9
|
+
*/
|
|
10
|
+
export function extractCursor(data) {
|
|
11
|
+
if (!data?.next) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
const urlObj = new URL(data.next);
|
|
16
|
+
const offset = urlObj.searchParams.get("offset");
|
|
17
|
+
return offset || undefined;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Helper function to convert unknown errors to SDK errors
|
|
25
|
+
*/
|
|
26
|
+
function normalizeError(error) {
|
|
27
|
+
if (error instanceof ZapierError) {
|
|
28
|
+
return error;
|
|
29
|
+
}
|
|
30
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
31
|
+
return new ZapierUnknownError(`Unknown error: ${message}`, { cause: error });
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Higher-order function that creates a function from a core function
|
|
35
|
+
*
|
|
36
|
+
* @param coreFn - Function that returns T directly or throws errors
|
|
37
|
+
* @param schema - Optional Zod schema for validation
|
|
38
|
+
* @returns A function that normalizes and rethrows errors
|
|
39
|
+
*/
|
|
40
|
+
export function createFunction(coreFn, schema) {
|
|
41
|
+
const functionName = coreFn.name;
|
|
42
|
+
// Create a named function using dynamic property access
|
|
43
|
+
const namedFunctions = {
|
|
44
|
+
[functionName]: async function (options) {
|
|
45
|
+
try {
|
|
46
|
+
// Convert undefined options to empty object
|
|
47
|
+
const normalizedOptions = (options ?? {});
|
|
48
|
+
if (schema) {
|
|
49
|
+
const validatedOptions = validateOptions(schema, normalizedOptions);
|
|
50
|
+
return await coreFn({
|
|
51
|
+
...normalizedOptions,
|
|
52
|
+
...validatedOptions,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return await coreFn(normalizedOptions);
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
// Catch and normalize any errors, then rethrow
|
|
59
|
+
throw normalizeError(error);
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
return namedFunctions[functionName];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Higher-order function that creates a page function that wraps results in {data}
|
|
67
|
+
* Internal utility used by createPaginatedFunction
|
|
68
|
+
*/
|
|
69
|
+
function createPageFunction(coreFn) {
|
|
70
|
+
const functionName = coreFn.name + "Page";
|
|
71
|
+
// Create a named function using dynamic property access
|
|
72
|
+
const namedFunctions = {
|
|
73
|
+
[functionName]: async function (options) {
|
|
74
|
+
try {
|
|
75
|
+
const result = await coreFn(options);
|
|
76
|
+
// If result already has a data property, ensure it's an array
|
|
77
|
+
if (result && typeof result === "object" && "data" in result) {
|
|
78
|
+
const data = result.data;
|
|
79
|
+
return {
|
|
80
|
+
data: Array.isArray(data) ? data : [data],
|
|
81
|
+
nextCursor: result.nextCursor,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
// If result is an array, wrap in {data: result}
|
|
85
|
+
if (Array.isArray(result)) {
|
|
86
|
+
return { data: result };
|
|
87
|
+
}
|
|
88
|
+
// If result is non-array, wrap in {data: [result]}
|
|
89
|
+
return { data: [result] };
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
// Catch and normalize any errors, then rethrow
|
|
93
|
+
throw normalizeError(error);
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
return namedFunctions[functionName];
|
|
98
|
+
}
|
|
99
|
+
export function createPaginatedFunction(coreFn, schema) {
|
|
100
|
+
const pageFunction = createPageFunction(coreFn);
|
|
101
|
+
const functionName = coreFn.name;
|
|
102
|
+
const validator = schema ? createValidator(schema) : null;
|
|
103
|
+
// Create the main paginated function
|
|
104
|
+
const namedFunctions = {
|
|
105
|
+
[functionName]: function (options) {
|
|
106
|
+
// Convert undefined options to empty object
|
|
107
|
+
const normalizedOptions = (options ?? {});
|
|
108
|
+
// Validate options if schema provided (validated fields take precedence)
|
|
109
|
+
const validatedOptions = {
|
|
110
|
+
...normalizedOptions,
|
|
111
|
+
...(validator ? validator(normalizedOptions) : normalizedOptions),
|
|
112
|
+
};
|
|
113
|
+
// Set default pageSize if not provided
|
|
114
|
+
const pageSize = validatedOptions.pageSize || 100;
|
|
115
|
+
const optimizedOptions = {
|
|
116
|
+
...validatedOptions,
|
|
117
|
+
pageSize,
|
|
118
|
+
};
|
|
119
|
+
const iterator = paginate(pageFunction, optimizedOptions);
|
|
120
|
+
// Get a promise for the first iteration
|
|
121
|
+
const firstPagePromise = iterator.next().then((result) => {
|
|
122
|
+
if (result.done) {
|
|
123
|
+
throw new Error("Paginate should always iterate at least once");
|
|
124
|
+
}
|
|
125
|
+
return result.value;
|
|
126
|
+
});
|
|
127
|
+
// Add Symbol.asyncIterator to make the promise iterable
|
|
128
|
+
return Object.assign(firstPagePromise, {
|
|
129
|
+
[Symbol.asyncIterator]: async function* () {
|
|
130
|
+
// Yield the first page (await the promise)
|
|
131
|
+
yield await firstPagePromise;
|
|
132
|
+
// Yield the rest of the pages
|
|
133
|
+
for await (const page of iterator) {
|
|
134
|
+
yield page;
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
items: function () {
|
|
138
|
+
return {
|
|
139
|
+
[Symbol.asyncIterator]: async function* () {
|
|
140
|
+
// Yield the first page items
|
|
141
|
+
const firstPage = await firstPagePromise;
|
|
142
|
+
for (const item of firstPage.data) {
|
|
143
|
+
yield item;
|
|
144
|
+
}
|
|
145
|
+
// Yield items from the rest of the pages
|
|
146
|
+
for await (const page of iterator) {
|
|
147
|
+
for (const item of page.data) {
|
|
148
|
+
yield item;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
return namedFunctions[functionName];
|
|
158
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-utils.test.d.ts","sourceRoot":"","sources":["../../src/utils/function-utils.test.ts"],"names":[],"mappings":""}
|