@nangohq/runner-sdk 0.48.3
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/action.d.ts +81 -0
- package/dist/action.js +243 -0
- package/dist/action.js.map +1 -0
- package/dist/dataValidation.d.ts +11 -0
- package/dist/dataValidation.js +56 -0
- package/dist/dataValidation.js.map +1 -0
- package/dist/dataValidation.unit.test.d.ts +1 -0
- package/dist/dataValidation.unit.test.js +140 -0
- package/dist/dataValidation.unit.test.js.map +1 -0
- package/dist/errors.d.ts +28 -0
- package/dist/errors.js +37 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/paginate.service.d.ts +12 -0
- package/dist/paginate.service.js +146 -0
- package/dist/paginate.service.js.map +1 -0
- package/dist/sdk.d.ts +111 -0
- package/dist/sdk.js +303 -0
- package/dist/sdk.js.map +1 -0
- package/dist/sync.d.ts +19 -0
- package/dist/sync.js +42 -0
- package/dist/sync.js.map +1 -0
- package/models.d.ts +422 -0
- package/package.json +26 -0
package/dist/action.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { Nango } from '@nangohq/node';
|
|
2
|
+
import type { AxiosResponse } from 'axios';
|
|
3
|
+
import type { ApiKeyCredentials, ApiPublicConnectionFull, AppCredentials, AppStoreCredentials, BasicApiCredentials, BillCredentials, CustomCredentials, EnvironmentVariable, GetPublicConnection, GetPublicIntegration, JwtCredentials, MaybePromise, Metadata, NangoProps, OAuth1Token, OAuth2ClientCredentials, SetMetadata, SignatureCredentials, TableauCredentials, TbaCredentials, TwoStepCredentials, UnauthCredentials, UpdateMetadata, UserLogParameters, UserProvidedProxyConfiguration } from '@nangohq/types';
|
|
4
|
+
import { ActionError } from './errors.js';
|
|
5
|
+
export type ProxyConfiguration = Omit<UserProvidedProxyConfiguration, 'files' | 'providerConfigKey' | 'connectionId'> & {
|
|
6
|
+
providerConfigKey?: string;
|
|
7
|
+
connectionId?: string;
|
|
8
|
+
};
|
|
9
|
+
export declare abstract class NangoActionBase {
|
|
10
|
+
abstract nango: Nango;
|
|
11
|
+
private attributes;
|
|
12
|
+
activityLogId?: string | undefined;
|
|
13
|
+
syncId?: string;
|
|
14
|
+
nangoConnectionId?: number;
|
|
15
|
+
environmentId: number;
|
|
16
|
+
environmentName?: string;
|
|
17
|
+
syncJobId?: number;
|
|
18
|
+
abortSignal?: NangoProps['abortSignal'];
|
|
19
|
+
syncConfig?: NangoProps['syncConfig'];
|
|
20
|
+
runnerFlags: NangoProps['runnerFlags'];
|
|
21
|
+
connectionId: string;
|
|
22
|
+
providerConfigKey: string;
|
|
23
|
+
provider?: string;
|
|
24
|
+
ActionError: typeof ActionError;
|
|
25
|
+
protected memoizedConnections: Map<string, {
|
|
26
|
+
connection: ApiPublicConnectionFull;
|
|
27
|
+
timestamp: number;
|
|
28
|
+
}>;
|
|
29
|
+
protected memoizedIntegration: Map<string, {
|
|
30
|
+
integration: GetPublicIntegration["Success"]["data"];
|
|
31
|
+
timestamp: number;
|
|
32
|
+
}>;
|
|
33
|
+
constructor(config: NangoProps);
|
|
34
|
+
protected stringify(): string;
|
|
35
|
+
protected proxyConfig(config: ProxyConfiguration): UserProvidedProxyConfiguration;
|
|
36
|
+
protected throwIfAborted(): void;
|
|
37
|
+
abstract proxy<T = any>(config: ProxyConfiguration): Promise<AxiosResponse<T>>;
|
|
38
|
+
get<T = any>(config: Omit<ProxyConfiguration, 'method'>): Promise<AxiosResponse<T>>;
|
|
39
|
+
post<T = any>(config: Omit<ProxyConfiguration, 'method'>): Promise<AxiosResponse<T>>;
|
|
40
|
+
put<T = any>(config: Omit<ProxyConfiguration, 'method'>): Promise<AxiosResponse<T>>;
|
|
41
|
+
patch<T = any>(config: Omit<ProxyConfiguration, 'method'>): Promise<AxiosResponse<T>>;
|
|
42
|
+
delete<T = any>(config: Omit<ProxyConfiguration, 'method'>): Promise<AxiosResponse<T>>;
|
|
43
|
+
getToken(): Promise<string | OAuth1Token | OAuth2ClientCredentials | BasicApiCredentials | ApiKeyCredentials | AppCredentials | AppStoreCredentials | UnauthCredentials | CustomCredentials | TbaCredentials | TableauCredentials | JwtCredentials | BillCredentials | TwoStepCredentials | SignatureCredentials>;
|
|
44
|
+
/**
|
|
45
|
+
* Get current integration
|
|
46
|
+
*/
|
|
47
|
+
getIntegration(queries?: GetPublicIntegration['Querystring']): Promise<GetPublicIntegration['Success']['data']>;
|
|
48
|
+
getConnection(providerConfigKeyOverride?: string, connectionIdOverride?: string): Promise<GetPublicConnection['Success']>;
|
|
49
|
+
setMetadata(metadata: Metadata): Promise<AxiosResponse<SetMetadata['Success']>>;
|
|
50
|
+
updateMetadata(metadata: Metadata): Promise<AxiosResponse<UpdateMetadata['Success']>>;
|
|
51
|
+
/**
|
|
52
|
+
* @deprecated please use setMetadata instead.
|
|
53
|
+
*/
|
|
54
|
+
setFieldMapping(fieldMapping: Record<string, string>): Promise<AxiosResponse<object>>;
|
|
55
|
+
getMetadata<T = Metadata>(): Promise<T>;
|
|
56
|
+
getWebhookURL(): Promise<string | null | undefined>;
|
|
57
|
+
/**
|
|
58
|
+
* @deprecated please use getMetadata instead.
|
|
59
|
+
*/
|
|
60
|
+
getFieldMapping(): Promise<Metadata>;
|
|
61
|
+
/**
|
|
62
|
+
* Log
|
|
63
|
+
* @desc Log a message to the activity log which shows up in the Nango Dashboard
|
|
64
|
+
* note that the last argument can be an object with a level property to specify the log level
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* await nango.log('This is a log message', { level: 'error' })
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
abstract log(message: any, options?: UserLogParameters | {
|
|
71
|
+
[key: string]: any;
|
|
72
|
+
level?: never;
|
|
73
|
+
}): MaybePromise<void>;
|
|
74
|
+
abstract log(message: string, ...args: [any, UserLogParameters]): MaybePromise<void>;
|
|
75
|
+
abstract log(...args: [...any]): MaybePromise<void>;
|
|
76
|
+
getEnvironmentVariables(): Promise<EnvironmentVariable[] | null>;
|
|
77
|
+
getFlowAttributes<A = object>(): A | null;
|
|
78
|
+
paginate<T = any>(config: ProxyConfiguration): AsyncGenerator<T[], undefined, void>;
|
|
79
|
+
triggerAction<In = unknown, Out = object>(providerConfigKey: string, connectionId: string, actionName: string, input?: In): Promise<Out>;
|
|
80
|
+
abstract triggerSync(providerConfigKey: string, connectionId: string, syncName: string, fullResync?: boolean): Promise<void | string>;
|
|
81
|
+
}
|
package/dist/action.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import paginateService from './paginate.service.js';
|
|
2
|
+
import { getProvider } from '@nangohq/providers';
|
|
3
|
+
import { AbortedSDKError, ActionError, UnknownProviderSDKError } from './errors.js';
|
|
4
|
+
const MEMOIZED_CONNECTION_TTL = 60000;
|
|
5
|
+
const MEMOIZED_INTEGRATION_TTL = 10 * 60 * 1000;
|
|
6
|
+
export class NangoActionBase {
|
|
7
|
+
attributes = {};
|
|
8
|
+
activityLogId;
|
|
9
|
+
syncId;
|
|
10
|
+
nangoConnectionId;
|
|
11
|
+
environmentId;
|
|
12
|
+
environmentName;
|
|
13
|
+
syncJobId;
|
|
14
|
+
abortSignal;
|
|
15
|
+
syncConfig;
|
|
16
|
+
runnerFlags;
|
|
17
|
+
connectionId;
|
|
18
|
+
providerConfigKey;
|
|
19
|
+
provider;
|
|
20
|
+
ActionError = ActionError;
|
|
21
|
+
memoizedConnections = new Map();
|
|
22
|
+
memoizedIntegration = new Map();
|
|
23
|
+
constructor(config) {
|
|
24
|
+
this.connectionId = config.connectionId;
|
|
25
|
+
this.environmentId = config.environmentId;
|
|
26
|
+
this.providerConfigKey = config.providerConfigKey;
|
|
27
|
+
this.runnerFlags = config.runnerFlags;
|
|
28
|
+
if (config.activityLogId) {
|
|
29
|
+
this.activityLogId = config.activityLogId;
|
|
30
|
+
}
|
|
31
|
+
if (config.syncId) {
|
|
32
|
+
this.syncId = config.syncId;
|
|
33
|
+
}
|
|
34
|
+
if (config.nangoConnectionId) {
|
|
35
|
+
this.nangoConnectionId = config.nangoConnectionId;
|
|
36
|
+
}
|
|
37
|
+
if (config.syncJobId) {
|
|
38
|
+
this.syncJobId = config.syncJobId;
|
|
39
|
+
}
|
|
40
|
+
if (config.environmentName) {
|
|
41
|
+
this.environmentName = config.environmentName;
|
|
42
|
+
}
|
|
43
|
+
if (config.provider) {
|
|
44
|
+
this.provider = config.provider;
|
|
45
|
+
}
|
|
46
|
+
if (config.attributes) {
|
|
47
|
+
this.attributes = config.attributes;
|
|
48
|
+
}
|
|
49
|
+
if (config.abortSignal) {
|
|
50
|
+
this.abortSignal = config.abortSignal;
|
|
51
|
+
}
|
|
52
|
+
if (config.syncConfig) {
|
|
53
|
+
this.syncConfig = config.syncConfig;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
stringify() {
|
|
57
|
+
return JSON.stringify(this, (key, value) => {
|
|
58
|
+
if (key === 'secretKey') {
|
|
59
|
+
return '********';
|
|
60
|
+
}
|
|
61
|
+
return value;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
proxyConfig(config) {
|
|
65
|
+
if (!config.connectionId && this.connectionId) {
|
|
66
|
+
config.connectionId = this.connectionId;
|
|
67
|
+
}
|
|
68
|
+
if (!config.providerConfigKey && this.providerConfigKey) {
|
|
69
|
+
config.providerConfigKey = this.providerConfigKey;
|
|
70
|
+
}
|
|
71
|
+
if (!config.connectionId) {
|
|
72
|
+
throw new Error('Missing connection id');
|
|
73
|
+
}
|
|
74
|
+
if (!config.providerConfigKey) {
|
|
75
|
+
throw new Error('Missing provider config key');
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
method: 'GET',
|
|
79
|
+
...config,
|
|
80
|
+
providerConfigKey: config.providerConfigKey,
|
|
81
|
+
connectionId: config.connectionId,
|
|
82
|
+
headers: {
|
|
83
|
+
...(config.headers || {}),
|
|
84
|
+
'user-agent': this.nango.userAgent
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
throwIfAborted() {
|
|
89
|
+
if (this.abortSignal?.aborted) {
|
|
90
|
+
throw new AbortedSDKError();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async get(config) {
|
|
94
|
+
return this.proxy({
|
|
95
|
+
...config,
|
|
96
|
+
method: 'GET'
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async post(config) {
|
|
100
|
+
return this.proxy({
|
|
101
|
+
...config,
|
|
102
|
+
method: 'POST'
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
async put(config) {
|
|
106
|
+
return this.proxy({
|
|
107
|
+
...config,
|
|
108
|
+
method: 'PUT'
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
async patch(config) {
|
|
112
|
+
return this.proxy({
|
|
113
|
+
...config,
|
|
114
|
+
method: 'PATCH'
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
async delete(config) {
|
|
118
|
+
return this.proxy({
|
|
119
|
+
...config,
|
|
120
|
+
method: 'DELETE'
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
async getToken() {
|
|
124
|
+
this.throwIfAborted();
|
|
125
|
+
return this.nango.getToken(this.providerConfigKey, this.connectionId);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Get current integration
|
|
129
|
+
*/
|
|
130
|
+
async getIntegration(queries) {
|
|
131
|
+
this.throwIfAborted();
|
|
132
|
+
const key = queries?.include?.join(',') || 'default';
|
|
133
|
+
const has = this.memoizedIntegration.get(key);
|
|
134
|
+
if (has && MEMOIZED_INTEGRATION_TTL > Date.now() - has.timestamp) {
|
|
135
|
+
return has.integration;
|
|
136
|
+
}
|
|
137
|
+
const { data: integration } = await this.nango.getIntegration({ uniqueKey: this.providerConfigKey }, queries);
|
|
138
|
+
this.memoizedIntegration.set(key, { integration, timestamp: Date.now() });
|
|
139
|
+
return integration;
|
|
140
|
+
}
|
|
141
|
+
async getConnection(providerConfigKeyOverride, connectionIdOverride) {
|
|
142
|
+
this.throwIfAborted();
|
|
143
|
+
const providerConfigKey = providerConfigKeyOverride || this.providerConfigKey;
|
|
144
|
+
const connectionId = connectionIdOverride || this.connectionId;
|
|
145
|
+
const credentialsPair = `${providerConfigKey}${connectionId}`;
|
|
146
|
+
const cachedConnection = this.memoizedConnections.get(credentialsPair);
|
|
147
|
+
if (!cachedConnection || Date.now() - cachedConnection.timestamp > MEMOIZED_CONNECTION_TTL) {
|
|
148
|
+
const connection = await this.nango.getConnection(providerConfigKey, connectionId);
|
|
149
|
+
this.memoizedConnections.set(credentialsPair, { connection, timestamp: Date.now() });
|
|
150
|
+
return connection;
|
|
151
|
+
}
|
|
152
|
+
return cachedConnection.connection;
|
|
153
|
+
}
|
|
154
|
+
async setMetadata(metadata) {
|
|
155
|
+
this.throwIfAborted();
|
|
156
|
+
try {
|
|
157
|
+
return await this.nango.setMetadata(this.providerConfigKey, this.connectionId, metadata);
|
|
158
|
+
}
|
|
159
|
+
finally {
|
|
160
|
+
this.memoizedConnections.delete(`${this.providerConfigKey}${this.connectionId}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async updateMetadata(metadata) {
|
|
164
|
+
this.throwIfAborted();
|
|
165
|
+
try {
|
|
166
|
+
return await this.nango.updateMetadata(this.providerConfigKey, this.connectionId, metadata);
|
|
167
|
+
}
|
|
168
|
+
finally {
|
|
169
|
+
this.memoizedConnections.delete(`${this.providerConfigKey}${this.connectionId}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* @deprecated please use setMetadata instead.
|
|
174
|
+
*/
|
|
175
|
+
async setFieldMapping(fieldMapping) {
|
|
176
|
+
console.warn('setFieldMapping is deprecated. Please use setMetadata instead.');
|
|
177
|
+
return this.setMetadata(fieldMapping);
|
|
178
|
+
}
|
|
179
|
+
async getMetadata() {
|
|
180
|
+
this.throwIfAborted();
|
|
181
|
+
return (await this.getConnection(this.providerConfigKey, this.connectionId)).metadata;
|
|
182
|
+
}
|
|
183
|
+
async getWebhookURL() {
|
|
184
|
+
this.throwIfAborted();
|
|
185
|
+
const integration = await this.getIntegration({ include: ['webhook'] });
|
|
186
|
+
return integration.webhook_url;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* @deprecated please use getMetadata instead.
|
|
190
|
+
*/
|
|
191
|
+
async getFieldMapping() {
|
|
192
|
+
console.warn('getFieldMapping is deprecated. Please use getMetadata instead.');
|
|
193
|
+
const metadata = await this.getMetadata();
|
|
194
|
+
return metadata['fieldMapping'] || {};
|
|
195
|
+
}
|
|
196
|
+
async getEnvironmentVariables() {
|
|
197
|
+
return await this.nango.getEnvironmentVariables();
|
|
198
|
+
}
|
|
199
|
+
getFlowAttributes() {
|
|
200
|
+
if (!this.syncJobId) {
|
|
201
|
+
throw new Error('There is no current sync to get attributes from');
|
|
202
|
+
}
|
|
203
|
+
return this.attributes;
|
|
204
|
+
}
|
|
205
|
+
async *paginate(config) {
|
|
206
|
+
const provider = getProvider(this.provider);
|
|
207
|
+
if (!provider) {
|
|
208
|
+
throw new UnknownProviderSDKError({ provider: this.provider });
|
|
209
|
+
}
|
|
210
|
+
const templatePaginationConfig = provider.proxy?.paginate;
|
|
211
|
+
if (!templatePaginationConfig && (!config.paginate || !config.paginate.type)) {
|
|
212
|
+
throw Error('There was no pagination configuration for this integration or configuration passed in.');
|
|
213
|
+
}
|
|
214
|
+
const paginationConfig = {
|
|
215
|
+
...(templatePaginationConfig || {}),
|
|
216
|
+
...(config.paginate || {})
|
|
217
|
+
};
|
|
218
|
+
paginateService.validateConfiguration(paginationConfig);
|
|
219
|
+
config.method = config.method || 'GET';
|
|
220
|
+
const configMethod = config.method.toLocaleLowerCase();
|
|
221
|
+
const passPaginationParamsInBody = ['post', 'put', 'patch'].includes(configMethod);
|
|
222
|
+
const updatedBodyOrParams = (passPaginationParamsInBody ? config.data : config.params) ?? {};
|
|
223
|
+
const limitParameterName = paginationConfig.limit_name_in_request;
|
|
224
|
+
if (paginationConfig['limit']) {
|
|
225
|
+
updatedBodyOrParams[limitParameterName] = paginationConfig['limit'];
|
|
226
|
+
}
|
|
227
|
+
const proxyConfig = this.proxyConfig(config);
|
|
228
|
+
switch (paginationConfig.type) {
|
|
229
|
+
case 'cursor':
|
|
230
|
+
return yield* paginateService.cursor(proxyConfig, paginationConfig, updatedBodyOrParams, passPaginationParamsInBody, this.proxy.bind(this));
|
|
231
|
+
case 'link':
|
|
232
|
+
return yield* paginateService.link(proxyConfig, paginationConfig, updatedBodyOrParams, passPaginationParamsInBody, this.proxy.bind(this));
|
|
233
|
+
case 'offset':
|
|
234
|
+
return yield* paginateService.offset(proxyConfig, paginationConfig, updatedBodyOrParams, passPaginationParamsInBody, this.proxy.bind(this));
|
|
235
|
+
default:
|
|
236
|
+
throw Error(`'${paginationConfig['type']}' pagination is not supported.}`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
async triggerAction(providerConfigKey, connectionId, actionName, input) {
|
|
240
|
+
return await this.nango.triggerAction(providerConfigKey, connectionId, actionName, input);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
//# sourceMappingURL=action.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"action.js","sourceRoot":"","sources":["../lib/action.ts"],"names":[],"mappings":"AAEA,OAAO,eAAe,MAAM,uBAAuB,CAAC;AA8BpD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAEpF,MAAM,uBAAuB,GAAG,KAAK,CAAC;AACtC,MAAM,wBAAwB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAOhD,MAAM,OAAgB,eAAe;IAEzB,UAAU,GAAG,EAAE,CAAC;IACxB,aAAa,CAAsB;IACnC,MAAM,CAAU;IAChB,iBAAiB,CAAU;IAC3B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AA0B,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;YACjJ,KAAK,QAAQ;gBACT,OAAO,KAAK,CAAC,CAAC,eAAe,CAAC,MAAM,CAAI,WAAW,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,0BAA0B,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;YACnJ;gBACI,MAAM,KAAK,CAAC,IAAI,gBAAgB,CAAC,MAAM,CAAC,iCAAiC,CAAC,CAAC;QACnF,CAAC;IACL,CAAC;IAEM,KAAK,CAAC,aAAa,CAA6B,iBAAyB,EAAE,YAAoB,EAAE,UAAkB,EAAE,KAAU;QAClI,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,iBAAiB,EAAE,YAAY,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;IAC9F,CAAC;CAGJ"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ErrorObject } from 'ajv';
|
|
2
|
+
import type { JSONSchema7 } from 'json-schema';
|
|
3
|
+
export interface ValidateProps {
|
|
4
|
+
version: string;
|
|
5
|
+
input: unknown;
|
|
6
|
+
modelName: string | null | undefined;
|
|
7
|
+
jsonSchema: JSONSchema7 | undefined | null;
|
|
8
|
+
}
|
|
9
|
+
export declare function clearValidationCache(): void;
|
|
10
|
+
export type ValidateDataError = ErrorObject | Error;
|
|
11
|
+
export declare function validateData({ version, input, modelName, jsonSchema }: ValidateProps): true | ValidateDataError[];
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Ajv } from 'ajv';
|
|
2
|
+
import addFormats from 'ajv-formats';
|
|
3
|
+
let ajv;
|
|
4
|
+
const cache = new Map();
|
|
5
|
+
export function clearValidationCache() {
|
|
6
|
+
cache.clear();
|
|
7
|
+
}
|
|
8
|
+
export function validateData({ version, input, modelName, jsonSchema }) {
|
|
9
|
+
if (!jsonSchema) {
|
|
10
|
+
// For legacy reason, not all scripts have a jsonSchema
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
if (!modelName) {
|
|
14
|
+
if (input) {
|
|
15
|
+
// Unexpected input while the script expect nothing
|
|
16
|
+
return [{ instancePath: '', keyword: 'type', message: 'must be empty', params: {}, schemaPath: '#/type' }];
|
|
17
|
+
}
|
|
18
|
+
// No expectation and no input, skip everything
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
if (!jsonSchema['definitions'][modelName]) {
|
|
22
|
+
// Unexpected input while the script expect nothing
|
|
23
|
+
return [new Error(`model_not_found_${modelName}`)];
|
|
24
|
+
}
|
|
25
|
+
if (!ajv) {
|
|
26
|
+
ajv = new Ajv({ allErrors: true, discriminator: true });
|
|
27
|
+
addFormats(ajv);
|
|
28
|
+
}
|
|
29
|
+
let validator;
|
|
30
|
+
try {
|
|
31
|
+
const key = `${modelName}-${version}`;
|
|
32
|
+
if (cache.has(key)) {
|
|
33
|
+
validator = cache.get(key);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
// append all definitions and set current model name as the entry point
|
|
37
|
+
validator = ajv.compile({ ...jsonSchema, ...jsonSchema['definitions'][modelName] });
|
|
38
|
+
cache.set(key, validator);
|
|
39
|
+
}
|
|
40
|
+
if (validator(input)) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
return [err instanceof Error ? err : new Error('failed_to_parse_json_schema')];
|
|
46
|
+
}
|
|
47
|
+
const bag = [];
|
|
48
|
+
for (const error of validator.errors) {
|
|
49
|
+
if (!error.message) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
bag.push(error);
|
|
53
|
+
}
|
|
54
|
+
return bag;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=dataValidation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dataValidation.js","sourceRoot":"","sources":["../lib/dataValidation.ts"],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;YACjB,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC;QAChC,CAAC;aAAM,CAAC;YACJ,uEAAuE;YACvE,SAAS,GAAG,GAAG,CAAC,OAAO,CAAC,EAAE,GAAI,UAAkB,EAAE,GAAI,UAAU,CAAC,aAAa,CAAE,CAAC,SAAS,CAAS,EAAE,CAAC,CAAC;YACvG,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QAC9B,CAAC;QAED,IAAI,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YACnB,OAAO,IAAI,CAAC;QAChB,CAAC;IACL,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACX,OAAO,CAAC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC,CAAC;IACnF,CAAC;IAED,MAAM,GAAG,GAAkB,EAAE,CAAC;IAC9B,KAAK,MAAM,KAAK,IAAI,SAAS,CAAC,MAAO,EAAE,CAAC;QACpC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;YACjB,SAAS;QACb,CAAC;QAED,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it } from 'vitest';
|
|
2
|
+
import { clearValidationCache, validateData } from './dataValidation.js';
|
|
3
|
+
describe('validateData', () => {
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
clearValidationCache();
|
|
6
|
+
});
|
|
7
|
+
it('should skip if no json schema ', () => {
|
|
8
|
+
const val = validateData({ version: '1', input: { foo: 'bar' }, modelName: 'Test', jsonSchema: undefined });
|
|
9
|
+
expect(val).toStrictEqual(true);
|
|
10
|
+
});
|
|
11
|
+
it('should return true if no error', () => {
|
|
12
|
+
const val = validateData({
|
|
13
|
+
version: '1',
|
|
14
|
+
input: { foo: 'bar' },
|
|
15
|
+
modelName: 'Test',
|
|
16
|
+
jsonSchema: { definitions: { Test: { type: 'object', properties: { foo: { type: 'string' } } } } }
|
|
17
|
+
});
|
|
18
|
+
expect(val).toStrictEqual(true);
|
|
19
|
+
});
|
|
20
|
+
it('should return an error', () => {
|
|
21
|
+
const val = validateData({
|
|
22
|
+
version: '1',
|
|
23
|
+
input: { foo: 'bar' },
|
|
24
|
+
modelName: 'Test',
|
|
25
|
+
jsonSchema: {
|
|
26
|
+
definitions: { Test: { type: 'object', properties: { foo: { type: 'number' } }, required: ['foo'], additionalProperties: false } }
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
expect(val).toStrictEqual([
|
|
30
|
+
{ instancePath: '/foo', keyword: 'type', message: 'must be number', params: { type: 'number' }, schemaPath: '#/properties/foo/type' }
|
|
31
|
+
]);
|
|
32
|
+
});
|
|
33
|
+
it('should support ref', () => {
|
|
34
|
+
const val = validateData({
|
|
35
|
+
version: '1',
|
|
36
|
+
input: { ref: { id: 'bar' } },
|
|
37
|
+
modelName: 'Test1',
|
|
38
|
+
jsonSchema: {
|
|
39
|
+
definitions: {
|
|
40
|
+
Test1: { type: 'object', properties: { ref: { $ref: '#/definitions/Test2' } }, required: ['ref'], additionalProperties: false },
|
|
41
|
+
Test2: { type: 'object', properties: { id: { type: 'string' } }, required: ['id'], additionalProperties: false }
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
expect(val).toStrictEqual(true);
|
|
46
|
+
});
|
|
47
|
+
it('should support ref error', () => {
|
|
48
|
+
const val = validateData({
|
|
49
|
+
version: '1',
|
|
50
|
+
input: { ref: { id: 1 } },
|
|
51
|
+
modelName: 'Test1',
|
|
52
|
+
jsonSchema: {
|
|
53
|
+
definitions: {
|
|
54
|
+
Test1: { type: 'object', properties: { ref: { $ref: '#/definitions/Test2' } }, required: ['ref'], additionalProperties: false },
|
|
55
|
+
Test2: { type: 'object', properties: { id: { type: 'string' } }, required: ['id'], additionalProperties: false }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
expect(val).toStrictEqual([
|
|
60
|
+
{
|
|
61
|
+
instancePath: '/ref/id',
|
|
62
|
+
keyword: 'type',
|
|
63
|
+
message: 'must be string',
|
|
64
|
+
params: { type: 'string' },
|
|
65
|
+
schemaPath: '#/definitions/Test2/properties/id/type'
|
|
66
|
+
}
|
|
67
|
+
]);
|
|
68
|
+
});
|
|
69
|
+
it('should not throw if invalid json schema', () => {
|
|
70
|
+
const val = validateData({
|
|
71
|
+
version: '1',
|
|
72
|
+
input: { foo: 'bar' },
|
|
73
|
+
modelName: 'Test',
|
|
74
|
+
jsonSchema: {
|
|
75
|
+
definitions: { Test: { type: 'object', properties: { ref: { $ref: '#/definitions/NotFound' } } } }
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
// Stringify because it's an exotic error object
|
|
79
|
+
expect(JSON.parse(JSON.stringify(val))).toStrictEqual([{ missingRef: '#/definitions/NotFound', missingSchema: '' }]);
|
|
80
|
+
});
|
|
81
|
+
it('should support exotic format', () => {
|
|
82
|
+
const val = validateData({
|
|
83
|
+
version: '1',
|
|
84
|
+
input: { foo: 'bar' },
|
|
85
|
+
modelName: 'Test',
|
|
86
|
+
jsonSchema: {
|
|
87
|
+
definitions: { Test: { type: 'object', properties: { date: { type: 'string', format: 'date-time' } } } }
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
expect(val).toStrictEqual(true);
|
|
91
|
+
});
|
|
92
|
+
it('should handle empty input with model', () => {
|
|
93
|
+
const val = validateData({
|
|
94
|
+
version: '1',
|
|
95
|
+
input: undefined,
|
|
96
|
+
modelName: 'Test',
|
|
97
|
+
jsonSchema: {
|
|
98
|
+
definitions: { Test: { type: 'object', properties: { date: { type: 'string', format: 'date-time' } } } }
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
expect(val).toStrictEqual([
|
|
102
|
+
{
|
|
103
|
+
instancePath: '',
|
|
104
|
+
keyword: 'type',
|
|
105
|
+
message: 'must be object',
|
|
106
|
+
params: {
|
|
107
|
+
type: 'object'
|
|
108
|
+
},
|
|
109
|
+
schemaPath: '#/type'
|
|
110
|
+
}
|
|
111
|
+
]);
|
|
112
|
+
});
|
|
113
|
+
it('should handle unexpected input', () => {
|
|
114
|
+
const val = validateData({
|
|
115
|
+
version: '1',
|
|
116
|
+
input: '1',
|
|
117
|
+
modelName: undefined,
|
|
118
|
+
jsonSchema: { definitions: {} }
|
|
119
|
+
});
|
|
120
|
+
expect(val).toStrictEqual([
|
|
121
|
+
{
|
|
122
|
+
instancePath: '',
|
|
123
|
+
keyword: 'type',
|
|
124
|
+
message: 'must be empty',
|
|
125
|
+
params: {},
|
|
126
|
+
schemaPath: '#/type'
|
|
127
|
+
}
|
|
128
|
+
]);
|
|
129
|
+
});
|
|
130
|
+
it('should handle unknown modelName', () => {
|
|
131
|
+
const val = validateData({
|
|
132
|
+
version: '1',
|
|
133
|
+
input: '1',
|
|
134
|
+
modelName: 'Test',
|
|
135
|
+
jsonSchema: { definitions: {} }
|
|
136
|
+
});
|
|
137
|
+
expect(val).toStrictEqual([new Error(`model_not_found_Test`)]);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
//# sourceMappingURL=dataValidation.unit.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dataValidation.unit.test.js","sourceRoot":"","sources":["../lib/dataValidation.unit.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC1D,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEzE,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC1B,UAAU,CAAC,GAAG,EAAE;QACZ,oBAAo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,KAAK,EAAE,GAAG;YACV,SAAS,EAAE,SAAS;YACpB,UAAU,EAAE,EAAE,WAAW,EAAE,EAAE,EAAE;SAClC,CAAC,CAAC;QACH,MAAM,CAAC,GAAG,CAAC,CAAC,aAAa,CAAC;YACtB;gBACI,YAAY,EAAE,EAAE;gBAChB,OAAO,EAAE,MAAM;gBACf,OAAO,EAAE,eAAe;gBACxB,MAAM,EAAE,EAAE;gBACV,UAAU,EAAE,QAAQ;aACvB;SACJ,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,iCAAiC,EAAE,GAAG,EAAE;QACvC,MAAM,GAAG,GAAG,YAAY,CAAC;YACrB,OAAO,EAAE,GAAG;YACZ,KAAK,EAAE,GAAG;YACV,SAAS,EAAE,MAAM;YACjB,UAAU,EAAE,EAAE,WAAW,EAAE,EAAE,EAAE;SAClC,CAAC,CAAC;QACH,MAAM,CAAC,GAAG,CAAC,CAAC,aAAa,CAAC,CAAC,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC;IACnE,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export declare abstract class SDKError extends Error {
|
|
2
|
+
abstract code: string;
|
|
3
|
+
payload: Record<string, unknown>;
|
|
4
|
+
constructor(payload?: Record<string, unknown>);
|
|
5
|
+
}
|
|
6
|
+
export declare class AbortedSDKError extends SDKError {
|
|
7
|
+
code: string;
|
|
8
|
+
}
|
|
9
|
+
export declare class UnknownProviderSDKError extends SDKError {
|
|
10
|
+
code: string;
|
|
11
|
+
}
|
|
12
|
+
export declare class InvalidRecordSDKError extends SDKError {
|
|
13
|
+
code: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class InvalidActionInputSDKError extends SDKError {
|
|
16
|
+
code: string;
|
|
17
|
+
}
|
|
18
|
+
export declare class InvalidActionOutputSDKError extends SDKError {
|
|
19
|
+
code: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* For external use only
|
|
23
|
+
*/
|
|
24
|
+
export declare class ActionError<T = Record<string, unknown>> extends Error {
|
|
25
|
+
type: string;
|
|
26
|
+
payload?: Record<string, unknown>;
|
|
27
|
+
constructor(payload?: T);
|
|
28
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export class SDKError extends Error {
|
|
2
|
+
payload;
|
|
3
|
+
constructor(payload) {
|
|
4
|
+
super();
|
|
5
|
+
this.payload = payload || {};
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export class AbortedSDKError extends SDKError {
|
|
9
|
+
code = 'script_aborted';
|
|
10
|
+
}
|
|
11
|
+
export class UnknownProviderSDKError extends SDKError {
|
|
12
|
+
code = 'unknown_provider_template_in_config';
|
|
13
|
+
}
|
|
14
|
+
export class InvalidRecordSDKError extends SDKError {
|
|
15
|
+
code = 'invalid_sync_record';
|
|
16
|
+
}
|
|
17
|
+
export class InvalidActionInputSDKError extends SDKError {
|
|
18
|
+
code = 'invalid_action_input';
|
|
19
|
+
}
|
|
20
|
+
export class InvalidActionOutputSDKError extends SDKError {
|
|
21
|
+
code = 'invalid_action_output';
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* For external use only
|
|
25
|
+
*/
|
|
26
|
+
export class ActionError extends Error {
|
|
27
|
+
type;
|
|
28
|
+
payload;
|
|
29
|
+
constructor(payload) {
|
|
30
|
+
super();
|
|
31
|
+
this.type = 'action_script_runtime_error';
|
|
32
|
+
if (payload) {
|
|
33
|
+
this.payload = payload;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../lib/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAgB,QAAS,SAAQ,KAAK;IAExC,OAAO,CAA0B;IAEjC,YAAY,OAAiC;QACzC,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,OAAO,GAAG,OAAO,IAAI,EAAE,CAAC;IACjC,CAAC;CACJ;AAED,MAAM,OAAO,eAAgB,SAAQ,QAAQ;IACzC,IAAI,GAAG,gBAAgB,CAAC;CAC3B;AAED,MAAM,OAAO,uBAAwB,SAAQ,QAAQ;IACjD,IAAI,GAAG,qCAAqC,CAAC;CAChD;AAED,MAAM,OAAO,qBAAsB,SAAQ,QAAQ;IAC/C,IAAI,GAAG,qBAAqB,CAAC;CAChC;AAED,MAAM,OAAO,0BAA2B,SAAQ,QAAQ;IACpD,IAAI,GAAG,sBAAsB,CAAC;CACjC;AAED,MAAM,OAAO,2BAA4B,SAAQ,QAAQ;IACrD,IAAI,GAAG,uBAAuB,CAAC;CAClC;AAED;;GAEG;AACH,MAAM,OAAO,WAAyC,SAAQ,KAAK;IAC/D,IAAI,CAAS;IACb,OAAO,CAA2B;IAElC,YAAY,OAAW;QACnB,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,IAAI,GAAG,6BAA6B,CAAC;QAC1C,IAAI,OAAO,EAAE,CAAC;YACV,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAC3B,CAAC;IACL,CAAC;CACJ"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC"}
|