@ankhorage/data-sources 0.6.0 → 1.0.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 +12 -0
- package/README.md +5 -5
- package/dist/discovery/index.d.ts +3 -3
- package/dist/discovery/index.d.ts.map +1 -1
- package/dist/discovery/index.js +19 -28
- package/dist/discovery/index.js.map +1 -1
- package/dist/generated-api/index.d.ts +10 -0
- package/dist/generated-api/index.d.ts.map +1 -0
- package/dist/generated-api/index.js +274 -0
- package/dist/generated-api/index.js.map +1 -0
- package/dist/generated-api/validateGeneratedApiCollection.d.ts +3 -0
- package/dist/generated-api/validateGeneratedApiCollection.d.ts.map +1 -0
- package/dist/generated-api/validateGeneratedApiCollection.js +35 -0
- package/dist/generated-api/validateGeneratedApiCollection.js.map +1 -0
- package/dist/graphql/index.d.ts +3 -3
- package/dist/graphql/index.d.ts.map +1 -1
- package/dist/graphql/index.js +24 -60
- package/dist/graphql/index.js.map +1 -1
- package/dist/index.d.ts +9 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -4
- package/dist/index.js.map +1 -1
- package/dist/openapi/index.d.ts +2 -2
- package/dist/openapi/index.d.ts.map +1 -1
- package/dist/openapi/index.js +19 -29
- package/dist/openapi/index.js.map +1 -1
- package/dist/rest/index.d.ts +3 -3
- package/dist/rest/index.d.ts.map +1 -1
- package/dist/rest/index.js +51 -51
- package/dist/rest/index.js.map +1 -1
- package/dist/test-runner/index.d.ts.map +1 -1
- package/dist/test-runner/index.js +60 -104
- package/dist/test-runner/index.js.map +1 -1
- package/package.json +2 -2
- package/src/discovery/index.test.ts +25 -101
- package/src/discovery/index.ts +22 -35
- package/src/{managed-api → generated-api}/index.test.ts +59 -67
- package/src/generated-api/index.ts +414 -0
- package/src/generated-api/validateGeneratedApiCollection.test.ts +91 -0
- package/src/generated-api/validateGeneratedApiCollection.ts +76 -0
- package/src/graphql/index.test.ts +28 -134
- package/src/graphql/index.ts +29 -89
- package/src/index.test.ts +25 -18
- package/src/index.ts +28 -7
- package/src/openapi/index.test.ts +36 -246
- package/src/openapi/index.ts +22 -60
- package/src/rest/index.test.ts +22 -173
- package/src/rest/index.ts +62 -59
- package/src/test-runner/index.test.ts +82 -150
- package/src/test-runner/index.ts +73 -143
- package/dist/managed-api/index.d.ts +0 -30
- package/dist/managed-api/index.d.ts.map +0 -1
- package/dist/managed-api/index.js +0 -257
- package/dist/managed-api/index.js.map +0 -1
- package/src/managed-api/index.ts +0 -364
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
export const MANAGED_API_CRUD_OPERATIONS = ['list', 'read', 'create', 'update', 'delete'];
|
|
2
|
-
export function createManagedApiDataSource(definition) {
|
|
3
|
-
const diagnostics = validateManagedApiDefinition(definition);
|
|
4
|
-
const hasErrors = diagnostics.some((diagnostic) => diagnostic.severity === 'error');
|
|
5
|
-
if (hasErrors)
|
|
6
|
-
return { ok: false, diagnostics };
|
|
7
|
-
return {
|
|
8
|
-
ok: true,
|
|
9
|
-
data: normalizeManagedApiDataSource(definition),
|
|
10
|
-
diagnostics,
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
export function validateManagedApiDefinition(definition) {
|
|
14
|
-
const diagnostics = [];
|
|
15
|
-
if (definition.resources.length === 0) {
|
|
16
|
-
diagnostics.push({
|
|
17
|
-
code: 'invalid-config',
|
|
18
|
-
dataSourceId: definition.id,
|
|
19
|
-
message: 'Managed API generation requires at least one resource.',
|
|
20
|
-
path: 'resources',
|
|
21
|
-
severity: 'error',
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
for (const resource of definition.resources) {
|
|
25
|
-
if (resource.name.trim().length === 0) {
|
|
26
|
-
diagnostics.push({
|
|
27
|
-
code: 'invalid-config',
|
|
28
|
-
dataSourceId: definition.id,
|
|
29
|
-
endpointId: resource.name,
|
|
30
|
-
message: 'Managed API resource names must be non-empty.',
|
|
31
|
-
path: 'resources.name',
|
|
32
|
-
severity: 'error',
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
if (resource.collection.name.trim().length === 0) {
|
|
36
|
-
diagnostics.push({
|
|
37
|
-
code: 'invalid-config',
|
|
38
|
-
dataSourceId: definition.id,
|
|
39
|
-
endpointId: resource.name,
|
|
40
|
-
message: 'Managed API resource collections must have a non-empty name.',
|
|
41
|
-
path: `resources.${resource.name}.collection.name`,
|
|
42
|
-
severity: 'error',
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
const primaryKey = resolveManagedApiPrimaryKey(resource.collection);
|
|
46
|
-
if (primaryKey === undefined &&
|
|
47
|
-
getManagedApiOperations(resource).some((operation) => operation !== 'list')) {
|
|
48
|
-
diagnostics.push({
|
|
49
|
-
code: 'invalid-config',
|
|
50
|
-
dataSourceId: definition.id,
|
|
51
|
-
endpointId: resource.name,
|
|
52
|
-
message: 'Managed API resources need a primaryKey field when generating read/create/update/delete operations.',
|
|
53
|
-
path: `resources.${resource.name}.collection.primaryKey`,
|
|
54
|
-
severity: 'error',
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return diagnostics;
|
|
59
|
-
}
|
|
60
|
-
export function normalizeManagedApiDataSource(definition) {
|
|
61
|
-
const resources = [];
|
|
62
|
-
const endpoints = {};
|
|
63
|
-
const schemas = {};
|
|
64
|
-
for (const resource of definition.resources) {
|
|
65
|
-
const operations = getManagedApiOperations(resource);
|
|
66
|
-
const resourceConfig = {
|
|
67
|
-
name: resource.name,
|
|
68
|
-
collection: resource.collection,
|
|
69
|
-
operations,
|
|
70
|
-
metadata: createManagedApiResourceMetadata(resource),
|
|
71
|
-
};
|
|
72
|
-
resources.push(resourceConfig);
|
|
73
|
-
endpoints[resource.name] = createManagedApiEndpoint(definition, resource, operations);
|
|
74
|
-
schemas[resource.name] = createManagedApiResourceSchema(resource.collection);
|
|
75
|
-
}
|
|
76
|
-
return {
|
|
77
|
-
id: definition.id,
|
|
78
|
-
kind: 'managed-api',
|
|
79
|
-
name: definition.name,
|
|
80
|
-
description: definition.description,
|
|
81
|
-
adapter: definition.adapter,
|
|
82
|
-
resources,
|
|
83
|
-
endpoints,
|
|
84
|
-
schemas,
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
export function createManagedApiEndpoint(definition, resource, operations = getManagedApiOperations(resource)) {
|
|
88
|
-
const operationConfigs = {};
|
|
89
|
-
for (const operation of operations) {
|
|
90
|
-
const operationConfig = createManagedApiOperation(definition, resource, operation);
|
|
91
|
-
operationConfigs[operationConfig.id] = operationConfig;
|
|
92
|
-
}
|
|
93
|
-
return {
|
|
94
|
-
id: resource.name,
|
|
95
|
-
kind: 'database',
|
|
96
|
-
operations: operationConfigs,
|
|
97
|
-
metadata: {
|
|
98
|
-
adapterId: definition.adapter.id,
|
|
99
|
-
collection: resource.collection.name,
|
|
100
|
-
schema: resource.collection.schema ?? null,
|
|
101
|
-
source: 'managed-api',
|
|
102
|
-
},
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
export function createManagedApiOperation(definition, resource, operation) {
|
|
106
|
-
const operationId = createManagedApiOperationId(resource.name, operation);
|
|
107
|
-
const primaryKey = resolveManagedApiPrimaryKey(resource.collection);
|
|
108
|
-
const primaryKeyParameter = primaryKey === undefined ? undefined : createPrimaryKeyParameter(primaryKey);
|
|
109
|
-
return {
|
|
110
|
-
id: operationId,
|
|
111
|
-
endpointId: resource.name,
|
|
112
|
-
name: createManagedApiOperationName(resource.name, operation),
|
|
113
|
-
protocol: 'database',
|
|
114
|
-
intent: mapManagedApiOperationIntent(operation),
|
|
115
|
-
request: createManagedApiOperationRequest(resource.collection, operation, primaryKeyParameter),
|
|
116
|
-
response: createManagedApiOperationResponse(resource.collection, operation),
|
|
117
|
-
metadata: {
|
|
118
|
-
adapterId: definition.adapter.id,
|
|
119
|
-
collection: resource.collection.name,
|
|
120
|
-
operation,
|
|
121
|
-
schema: resource.collection.schema ?? null,
|
|
122
|
-
source: 'managed-api',
|
|
123
|
-
},
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
export function getManagedApiOperations(resource) {
|
|
127
|
-
return resource.operations ?? MANAGED_API_CRUD_OPERATIONS;
|
|
128
|
-
}
|
|
129
|
-
export function createManagedApiOperationId(resourceName, operation) {
|
|
130
|
-
return `${resourceName}.${operation}`;
|
|
131
|
-
}
|
|
132
|
-
export function createManagedApiResourceSchema(collection) {
|
|
133
|
-
const properties = {};
|
|
134
|
-
const required = [];
|
|
135
|
-
for (const field of collection.fields) {
|
|
136
|
-
properties[field.name] = createManagedApiFieldSchema(field);
|
|
137
|
-
if (field.required === true)
|
|
138
|
-
required.push(field.name);
|
|
139
|
-
}
|
|
140
|
-
return {
|
|
141
|
-
type: 'object',
|
|
142
|
-
title: collection.name,
|
|
143
|
-
required: required.length > 0 ? required : undefined,
|
|
144
|
-
properties,
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
function createManagedApiResourceMetadata(resource) {
|
|
148
|
-
if (resource.policies === undefined)
|
|
149
|
-
return undefined;
|
|
150
|
-
return {
|
|
151
|
-
policies: resource.policies.map((policy) => {
|
|
152
|
-
const metadata = {
|
|
153
|
-
id: policy.id,
|
|
154
|
-
};
|
|
155
|
-
if (policy.operation !== undefined) {
|
|
156
|
-
metadata.operation = policy.operation;
|
|
157
|
-
}
|
|
158
|
-
return metadata;
|
|
159
|
-
}),
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
function createManagedApiOperationName(resourceName, operation) {
|
|
163
|
-
return `${operation} ${resourceName}`;
|
|
164
|
-
}
|
|
165
|
-
function mapManagedApiOperationIntent(operation) {
|
|
166
|
-
if (operation === 'list' || operation === 'read')
|
|
167
|
-
return 'read';
|
|
168
|
-
if (operation === 'create')
|
|
169
|
-
return 'create';
|
|
170
|
-
if (operation === 'update')
|
|
171
|
-
return 'update';
|
|
172
|
-
return 'delete';
|
|
173
|
-
}
|
|
174
|
-
function createManagedApiOperationRequest(collection, operation, primaryKeyParameter) {
|
|
175
|
-
if (operation === 'list') {
|
|
176
|
-
return {
|
|
177
|
-
parameters: [
|
|
178
|
-
{
|
|
179
|
-
name: 'limit',
|
|
180
|
-
location: 'query',
|
|
181
|
-
schema: { type: 'integer' },
|
|
182
|
-
},
|
|
183
|
-
{
|
|
184
|
-
name: 'offset',
|
|
185
|
-
location: 'query',
|
|
186
|
-
schema: { type: 'integer' },
|
|
187
|
-
},
|
|
188
|
-
],
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
if (operation === 'read' || operation === 'delete') {
|
|
192
|
-
return primaryKeyParameter === undefined ? undefined : { parameters: [primaryKeyParameter] };
|
|
193
|
-
}
|
|
194
|
-
const parameters = operation === 'update' && primaryKeyParameter !== undefined ? [primaryKeyParameter] : undefined;
|
|
195
|
-
return {
|
|
196
|
-
parameters,
|
|
197
|
-
schema: createManagedApiResourceSchema(collection),
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
function createManagedApiOperationResponse(collection, operation) {
|
|
201
|
-
const resourceSchema = createManagedApiResourceSchema(collection);
|
|
202
|
-
if (operation === 'list') {
|
|
203
|
-
return {
|
|
204
|
-
schema: {
|
|
205
|
-
type: 'array',
|
|
206
|
-
items: resourceSchema,
|
|
207
|
-
},
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
if (operation === 'delete') {
|
|
211
|
-
return {
|
|
212
|
-
schema: {
|
|
213
|
-
type: 'object',
|
|
214
|
-
properties: {
|
|
215
|
-
deleted: { type: 'boolean' },
|
|
216
|
-
},
|
|
217
|
-
},
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
return { schema: resourceSchema };
|
|
221
|
-
}
|
|
222
|
-
function createPrimaryKeyParameter(primaryKey) {
|
|
223
|
-
return {
|
|
224
|
-
name: primaryKey,
|
|
225
|
-
location: 'path',
|
|
226
|
-
required: true,
|
|
227
|
-
schema: {
|
|
228
|
-
type: 'string',
|
|
229
|
-
},
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
function resolveManagedApiPrimaryKey(collection) {
|
|
233
|
-
if (collection.primaryKey !== undefined)
|
|
234
|
-
return collection.primaryKey;
|
|
235
|
-
return collection.fields.find((field) => field.unique === true)?.name;
|
|
236
|
-
}
|
|
237
|
-
function createManagedApiFieldSchema(field) {
|
|
238
|
-
const schema = mapDbFieldTypeToDataSchema(field.type);
|
|
239
|
-
return {
|
|
240
|
-
...schema,
|
|
241
|
-
default: field.defaultValue,
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
function mapDbFieldTypeToDataSchema(type) {
|
|
245
|
-
if (type === 'boolean')
|
|
246
|
-
return { type: 'boolean' };
|
|
247
|
-
if (type === 'datetime')
|
|
248
|
-
return { type: 'string', format: 'date-time' };
|
|
249
|
-
if (type === 'json')
|
|
250
|
-
return { type: 'object' };
|
|
251
|
-
if (type === 'number')
|
|
252
|
-
return { type: 'number' };
|
|
253
|
-
if (type === 'uuid')
|
|
254
|
-
return { type: 'string', format: 'uuid' };
|
|
255
|
-
return { type: 'string' };
|
|
256
|
-
}
|
|
257
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/managed-api/index.ts"],"names":[],"mappings":"AAoBA,MAAM,CAAC,MAAM,2BAA2B,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,CAAU,CAAC;AAwBnG,MAAM,UAAU,0BAA0B,CACxC,UAA0C;IAE1C,MAAM,WAAW,GAAG,4BAA4B,CAAC,UAAU,CAAC,CAAC;IAC7D,MAAM,SAAS,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC;IAEpF,IAAI,SAAS;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC;IAEjD,OAAO;QACL,EAAE,EAAE,IAAI;QACR,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AAC;AACJ,CAAC;AAED,SAAS,2BAA2B,CAAC,UAAkC;IACrE,IAAI,UAAU,CAAC,UAAU,KAAK,SAAS;QAAE,OAAO,UAAU,CAAC,UAAU,CAAC;IACtE,OAAO,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,KAAK,IAAI,CAAC,EAAE,IAAI,CAAC;AACxE,CAAC;AAED,SAAS,2BAA2B,CAAC,KAAwB;IAC3D,MAAM,MAAM,GAAe,0BAA0B,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAElE,OAAO;QACL,GAAG,MAAM;QACT,OAAO,EAAE,KAAK,CAAC,YAAY;KAC5B,CAAC;AACJ,CAAC;AAED,SAAS,0BAA0B,CAAC,IAAiB;IACnD,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;IACnD,IAAI,IAAI,KAAK,UAAU;QAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC;IACxE,IAAI,IAAI,KAAK,MAAM;QAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;IAC/C,IAAI,IAAI,KAAK,QAAQ;QAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;IACjD,IAAI,IAAI,KAAK,MAAM;QAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;IAC/D,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;AAC5B,CAAC","sourcesContent":["import type {\n AdapterRef,\n DataContractValue,\n DataEndpointConfig,\n DataOperationConfig,\n DataOperationIntent,\n DataOperationParameter,\n DataSchema,\n DataSourceDiagnostic,\n DataSourceDiagnosticResult,\n ManagedApiDataSourceConfig,\n ManagedApiResourceConfig,\n OperationId,\n} from '@ankhorage/contracts/data';\nimport type {\n DbCollectionDefinition,\n DbFieldDefinition,\n DbFieldType,\n} from '@ankhorage/contracts/db';\n\nexport const MANAGED_API_CRUD_OPERATIONS = ['list', 'read', 'create', 'update', 'delete'] as const;\n\nexport type ManagedApiCrudOperation = (typeof MANAGED_API_CRUD_OPERATIONS)[number];\n\nexport interface ManagedApiOperationPolicyRef {\n readonly id: string;\n readonly operation?: string;\n}\n\nexport interface ManagedApiGenerationResourceDefinition {\n readonly name: string;\n readonly collection: DbCollectionDefinition;\n readonly operations?: readonly ManagedApiCrudOperation[];\n readonly policies?: readonly ManagedApiOperationPolicyRef[];\n}\n\nexport interface ManagedApiGenerationDefinition {\n readonly id: string;\n readonly adapter: AdapterRef;\n readonly name?: string;\n readonly description?: string;\n readonly resources: readonly ManagedApiGenerationResourceDefinition[];\n}\n\nexport function createManagedApiDataSource(\n definition: ManagedApiGenerationDefinition,\n): DataSourceDiagnosticResult<ManagedApiDataSourceConfig> {\n const diagnostics = validateManagedApiDefinition(definition);\n const hasErrors = diagnostics.some((diagnostic) => diagnostic.severity === 'error');\n\n if (hasErrors) return { ok: false, diagnostics };\n\n return {\n ok: true,\n data: normalizeManagedApiDataSource(definition),\n diagnostics,\n };\n}\n\nexport function validateManagedApiDefinition(\n definition: ManagedApiGenerationDefinition,\n): readonly DataSourceDiagnostic[] {\n const diagnostics: DataSourceDiagnostic[] = [];\n\n if (definition.resources.length === 0) {\n diagnostics.push({\n code: 'invalid-config',\n dataSourceId: definition.id,\n message: 'Managed API generation requires at least one resource.',\n path: 'resources',\n severity: 'error',\n });\n }\n\n for (const resource of definition.resources) {\n if (resource.name.trim().length === 0) {\n diagnostics.push({\n code: 'invalid-config',\n dataSourceId: definition.id,\n endpointId: resource.name,\n message: 'Managed API resource names must be non-empty.',\n path: 'resources.name',\n severity: 'error',\n });\n }\n\n if (resource.collection.name.trim().length === 0) {\n diagnostics.push({\n code: 'invalid-config',\n dataSourceId: definition.id,\n endpointId: resource.name,\n message: 'Managed API resource collections must have a non-empty name.',\n path: `resources.${resource.name}.collection.name`,\n severity: 'error',\n });\n }\n\n const primaryKey = resolveManagedApiPrimaryKey(resource.collection);\n if (\n primaryKey === undefined &&\n getManagedApiOperations(resource).some((operation) => operation !== 'list')\n ) {\n diagnostics.push({\n code: 'invalid-config',\n dataSourceId: definition.id,\n endpointId: resource.name,\n message:\n 'Managed API resources need a primaryKey field when generating read/create/update/delete operations.',\n path: `resources.${resource.name}.collection.primaryKey`,\n severity: 'error',\n });\n }\n }\n\n return diagnostics;\n}\n\nexport function normalizeManagedApiDataSource(\n definition: ManagedApiGenerationDefinition,\n): ManagedApiDataSourceConfig {\n const resources: ManagedApiResourceConfig[] = [];\n const endpoints: Record<string, DataEndpointConfig> = {};\n const schemas: Record<string, DataSchema> = {};\n\n for (const resource of definition.resources) {\n const operations = getManagedApiOperations(resource);\n const resourceConfig: ManagedApiResourceConfig = {\n name: resource.name,\n collection: resource.collection,\n operations,\n metadata: createManagedApiResourceMetadata(resource),\n };\n\n resources.push(resourceConfig);\n endpoints[resource.name] = createManagedApiEndpoint(definition, resource, operations);\n schemas[resource.name] = createManagedApiResourceSchema(resource.collection);\n }\n\n return {\n id: definition.id,\n kind: 'managed-api',\n name: definition.name,\n description: definition.description,\n adapter: definition.adapter,\n resources,\n endpoints,\n schemas,\n };\n}\n\nexport function createManagedApiEndpoint(\n definition: ManagedApiGenerationDefinition,\n resource: ManagedApiGenerationResourceDefinition,\n operations: readonly ManagedApiCrudOperation[] = getManagedApiOperations(resource),\n): DataEndpointConfig {\n const operationConfigs: Record<OperationId, DataOperationConfig> = {};\n\n for (const operation of operations) {\n const operationConfig = createManagedApiOperation(definition, resource, operation);\n operationConfigs[operationConfig.id] = operationConfig;\n }\n\n return {\n id: resource.name,\n kind: 'database',\n operations: operationConfigs,\n metadata: {\n adapterId: definition.adapter.id,\n collection: resource.collection.name,\n schema: resource.collection.schema ?? null,\n source: 'managed-api',\n },\n };\n}\n\nexport function createManagedApiOperation(\n definition: ManagedApiGenerationDefinition,\n resource: ManagedApiGenerationResourceDefinition,\n operation: ManagedApiCrudOperation,\n): DataOperationConfig {\n const operationId = createManagedApiOperationId(resource.name, operation);\n const primaryKey = resolveManagedApiPrimaryKey(resource.collection);\n const primaryKeyParameter =\n primaryKey === undefined ? undefined : createPrimaryKeyParameter(primaryKey);\n\n return {\n id: operationId,\n endpointId: resource.name,\n name: createManagedApiOperationName(resource.name, operation),\n protocol: 'database',\n intent: mapManagedApiOperationIntent(operation),\n request: createManagedApiOperationRequest(resource.collection, operation, primaryKeyParameter),\n response: createManagedApiOperationResponse(resource.collection, operation),\n metadata: {\n adapterId: definition.adapter.id,\n collection: resource.collection.name,\n operation,\n schema: resource.collection.schema ?? null,\n source: 'managed-api',\n },\n };\n}\n\nexport function getManagedApiOperations(\n resource: ManagedApiGenerationResourceDefinition,\n): readonly ManagedApiCrudOperation[] {\n return resource.operations ?? MANAGED_API_CRUD_OPERATIONS;\n}\n\nexport function createManagedApiOperationId(\n resourceName: string,\n operation: ManagedApiCrudOperation,\n): OperationId {\n return `${resourceName}.${operation}`;\n}\n\nexport function createManagedApiResourceSchema(collection: DbCollectionDefinition): DataSchema {\n const properties: Record<string, DataSchema> = {};\n const required: string[] = [];\n\n for (const field of collection.fields) {\n properties[field.name] = createManagedApiFieldSchema(field);\n if (field.required === true) required.push(field.name);\n }\n\n return {\n type: 'object',\n title: collection.name,\n required: required.length > 0 ? required : undefined,\n properties,\n };\n}\n\nfunction createManagedApiResourceMetadata(\n resource: ManagedApiGenerationResourceDefinition,\n): DataContractValue | undefined {\n if (resource.policies === undefined) return undefined;\n\n return {\n policies: resource.policies.map((policy) => {\n const metadata: Record<string, DataContractValue> = {\n id: policy.id,\n };\n\n if (policy.operation !== undefined) {\n metadata.operation = policy.operation;\n }\n\n return metadata;\n }),\n };\n}\n\nfunction createManagedApiOperationName(\n resourceName: string,\n operation: ManagedApiCrudOperation,\n): string {\n return `${operation} ${resourceName}`;\n}\n\nfunction mapManagedApiOperationIntent(operation: ManagedApiCrudOperation): DataOperationIntent {\n if (operation === 'list' || operation === 'read') return 'read';\n if (operation === 'create') return 'create';\n if (operation === 'update') return 'update';\n return 'delete';\n}\n\nfunction createManagedApiOperationRequest(\n collection: DbCollectionDefinition,\n operation: ManagedApiCrudOperation,\n primaryKeyParameter: DataOperationParameter | undefined,\n): DataOperationConfig['request'] {\n if (operation === 'list') {\n return {\n parameters: [\n {\n name: 'limit',\n location: 'query',\n schema: { type: 'integer' },\n },\n {\n name: 'offset',\n location: 'query',\n schema: { type: 'integer' },\n },\n ],\n };\n }\n\n if (operation === 'read' || operation === 'delete') {\n return primaryKeyParameter === undefined ? undefined : { parameters: [primaryKeyParameter] };\n }\n\n const parameters =\n operation === 'update' && primaryKeyParameter !== undefined ? [primaryKeyParameter] : undefined;\n\n return {\n parameters,\n schema: createManagedApiResourceSchema(collection),\n };\n}\n\nfunction createManagedApiOperationResponse(\n collection: DbCollectionDefinition,\n operation: ManagedApiCrudOperation,\n): DataOperationConfig['response'] {\n const resourceSchema = createManagedApiResourceSchema(collection);\n\n if (operation === 'list') {\n return {\n schema: {\n type: 'array',\n items: resourceSchema,\n },\n };\n }\n\n if (operation === 'delete') {\n return {\n schema: {\n type: 'object',\n properties: {\n deleted: { type: 'boolean' },\n },\n },\n };\n }\n\n return { schema: resourceSchema };\n}\n\nfunction createPrimaryKeyParameter(primaryKey: string): DataOperationParameter {\n return {\n name: primaryKey,\n location: 'path',\n required: true,\n schema: {\n type: 'string',\n },\n };\n}\n\nfunction resolveManagedApiPrimaryKey(collection: DbCollectionDefinition): string | undefined {\n if (collection.primaryKey !== undefined) return collection.primaryKey;\n return collection.fields.find((field) => field.unique === true)?.name;\n}\n\nfunction createManagedApiFieldSchema(field: DbFieldDefinition): DataSchema {\n const schema: DataSchema = mapDbFieldTypeToDataSchema(field.type);\n\n return {\n ...schema,\n default: field.defaultValue,\n };\n}\n\nfunction mapDbFieldTypeToDataSchema(type: DbFieldType): DataSchema {\n if (type === 'boolean') return { type: 'boolean' };\n if (type === 'datetime') return { type: 'string', format: 'date-time' };\n if (type === 'json') return { type: 'object' };\n if (type === 'number') return { type: 'number' };\n if (type === 'uuid') return { type: 'string', format: 'uuid' };\n return { type: 'string' };\n}\n"]}
|
package/src/managed-api/index.ts
DELETED
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AdapterRef,
|
|
3
|
-
DataContractValue,
|
|
4
|
-
DataEndpointConfig,
|
|
5
|
-
DataOperationConfig,
|
|
6
|
-
DataOperationIntent,
|
|
7
|
-
DataOperationParameter,
|
|
8
|
-
DataSchema,
|
|
9
|
-
DataSourceDiagnostic,
|
|
10
|
-
DataSourceDiagnosticResult,
|
|
11
|
-
ManagedApiDataSourceConfig,
|
|
12
|
-
ManagedApiResourceConfig,
|
|
13
|
-
OperationId,
|
|
14
|
-
} from '@ankhorage/contracts/data';
|
|
15
|
-
import type {
|
|
16
|
-
DbCollectionDefinition,
|
|
17
|
-
DbFieldDefinition,
|
|
18
|
-
DbFieldType,
|
|
19
|
-
} from '@ankhorage/contracts/db';
|
|
20
|
-
|
|
21
|
-
export const MANAGED_API_CRUD_OPERATIONS = ['list', 'read', 'create', 'update', 'delete'] as const;
|
|
22
|
-
|
|
23
|
-
export type ManagedApiCrudOperation = (typeof MANAGED_API_CRUD_OPERATIONS)[number];
|
|
24
|
-
|
|
25
|
-
export interface ManagedApiOperationPolicyRef {
|
|
26
|
-
readonly id: string;
|
|
27
|
-
readonly operation?: string;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface ManagedApiGenerationResourceDefinition {
|
|
31
|
-
readonly name: string;
|
|
32
|
-
readonly collection: DbCollectionDefinition;
|
|
33
|
-
readonly operations?: readonly ManagedApiCrudOperation[];
|
|
34
|
-
readonly policies?: readonly ManagedApiOperationPolicyRef[];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface ManagedApiGenerationDefinition {
|
|
38
|
-
readonly id: string;
|
|
39
|
-
readonly adapter: AdapterRef;
|
|
40
|
-
readonly name?: string;
|
|
41
|
-
readonly description?: string;
|
|
42
|
-
readonly resources: readonly ManagedApiGenerationResourceDefinition[];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function createManagedApiDataSource(
|
|
46
|
-
definition: ManagedApiGenerationDefinition,
|
|
47
|
-
): DataSourceDiagnosticResult<ManagedApiDataSourceConfig> {
|
|
48
|
-
const diagnostics = validateManagedApiDefinition(definition);
|
|
49
|
-
const hasErrors = diagnostics.some((diagnostic) => diagnostic.severity === 'error');
|
|
50
|
-
|
|
51
|
-
if (hasErrors) return { ok: false, diagnostics };
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
ok: true,
|
|
55
|
-
data: normalizeManagedApiDataSource(definition),
|
|
56
|
-
diagnostics,
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function validateManagedApiDefinition(
|
|
61
|
-
definition: ManagedApiGenerationDefinition,
|
|
62
|
-
): readonly DataSourceDiagnostic[] {
|
|
63
|
-
const diagnostics: DataSourceDiagnostic[] = [];
|
|
64
|
-
|
|
65
|
-
if (definition.resources.length === 0) {
|
|
66
|
-
diagnostics.push({
|
|
67
|
-
code: 'invalid-config',
|
|
68
|
-
dataSourceId: definition.id,
|
|
69
|
-
message: 'Managed API generation requires at least one resource.',
|
|
70
|
-
path: 'resources',
|
|
71
|
-
severity: 'error',
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
for (const resource of definition.resources) {
|
|
76
|
-
if (resource.name.trim().length === 0) {
|
|
77
|
-
diagnostics.push({
|
|
78
|
-
code: 'invalid-config',
|
|
79
|
-
dataSourceId: definition.id,
|
|
80
|
-
endpointId: resource.name,
|
|
81
|
-
message: 'Managed API resource names must be non-empty.',
|
|
82
|
-
path: 'resources.name',
|
|
83
|
-
severity: 'error',
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (resource.collection.name.trim().length === 0) {
|
|
88
|
-
diagnostics.push({
|
|
89
|
-
code: 'invalid-config',
|
|
90
|
-
dataSourceId: definition.id,
|
|
91
|
-
endpointId: resource.name,
|
|
92
|
-
message: 'Managed API resource collections must have a non-empty name.',
|
|
93
|
-
path: `resources.${resource.name}.collection.name`,
|
|
94
|
-
severity: 'error',
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const primaryKey = resolveManagedApiPrimaryKey(resource.collection);
|
|
99
|
-
if (
|
|
100
|
-
primaryKey === undefined &&
|
|
101
|
-
getManagedApiOperations(resource).some((operation) => operation !== 'list')
|
|
102
|
-
) {
|
|
103
|
-
diagnostics.push({
|
|
104
|
-
code: 'invalid-config',
|
|
105
|
-
dataSourceId: definition.id,
|
|
106
|
-
endpointId: resource.name,
|
|
107
|
-
message:
|
|
108
|
-
'Managed API resources need a primaryKey field when generating read/create/update/delete operations.',
|
|
109
|
-
path: `resources.${resource.name}.collection.primaryKey`,
|
|
110
|
-
severity: 'error',
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return diagnostics;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export function normalizeManagedApiDataSource(
|
|
119
|
-
definition: ManagedApiGenerationDefinition,
|
|
120
|
-
): ManagedApiDataSourceConfig {
|
|
121
|
-
const resources: ManagedApiResourceConfig[] = [];
|
|
122
|
-
const endpoints: Record<string, DataEndpointConfig> = {};
|
|
123
|
-
const schemas: Record<string, DataSchema> = {};
|
|
124
|
-
|
|
125
|
-
for (const resource of definition.resources) {
|
|
126
|
-
const operations = getManagedApiOperations(resource);
|
|
127
|
-
const resourceConfig: ManagedApiResourceConfig = {
|
|
128
|
-
name: resource.name,
|
|
129
|
-
collection: resource.collection,
|
|
130
|
-
operations,
|
|
131
|
-
metadata: createManagedApiResourceMetadata(resource),
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
resources.push(resourceConfig);
|
|
135
|
-
endpoints[resource.name] = createManagedApiEndpoint(definition, resource, operations);
|
|
136
|
-
schemas[resource.name] = createManagedApiResourceSchema(resource.collection);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return {
|
|
140
|
-
id: definition.id,
|
|
141
|
-
kind: 'managed-api',
|
|
142
|
-
name: definition.name,
|
|
143
|
-
description: definition.description,
|
|
144
|
-
adapter: definition.adapter,
|
|
145
|
-
resources,
|
|
146
|
-
endpoints,
|
|
147
|
-
schemas,
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export function createManagedApiEndpoint(
|
|
152
|
-
definition: ManagedApiGenerationDefinition,
|
|
153
|
-
resource: ManagedApiGenerationResourceDefinition,
|
|
154
|
-
operations: readonly ManagedApiCrudOperation[] = getManagedApiOperations(resource),
|
|
155
|
-
): DataEndpointConfig {
|
|
156
|
-
const operationConfigs: Record<OperationId, DataOperationConfig> = {};
|
|
157
|
-
|
|
158
|
-
for (const operation of operations) {
|
|
159
|
-
const operationConfig = createManagedApiOperation(definition, resource, operation);
|
|
160
|
-
operationConfigs[operationConfig.id] = operationConfig;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
return {
|
|
164
|
-
id: resource.name,
|
|
165
|
-
kind: 'database',
|
|
166
|
-
operations: operationConfigs,
|
|
167
|
-
metadata: {
|
|
168
|
-
adapterId: definition.adapter.id,
|
|
169
|
-
collection: resource.collection.name,
|
|
170
|
-
schema: resource.collection.schema ?? null,
|
|
171
|
-
source: 'managed-api',
|
|
172
|
-
},
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
export function createManagedApiOperation(
|
|
177
|
-
definition: ManagedApiGenerationDefinition,
|
|
178
|
-
resource: ManagedApiGenerationResourceDefinition,
|
|
179
|
-
operation: ManagedApiCrudOperation,
|
|
180
|
-
): DataOperationConfig {
|
|
181
|
-
const operationId = createManagedApiOperationId(resource.name, operation);
|
|
182
|
-
const primaryKey = resolveManagedApiPrimaryKey(resource.collection);
|
|
183
|
-
const primaryKeyParameter =
|
|
184
|
-
primaryKey === undefined ? undefined : createPrimaryKeyParameter(primaryKey);
|
|
185
|
-
|
|
186
|
-
return {
|
|
187
|
-
id: operationId,
|
|
188
|
-
endpointId: resource.name,
|
|
189
|
-
name: createManagedApiOperationName(resource.name, operation),
|
|
190
|
-
protocol: 'database',
|
|
191
|
-
intent: mapManagedApiOperationIntent(operation),
|
|
192
|
-
request: createManagedApiOperationRequest(resource.collection, operation, primaryKeyParameter),
|
|
193
|
-
response: createManagedApiOperationResponse(resource.collection, operation),
|
|
194
|
-
metadata: {
|
|
195
|
-
adapterId: definition.adapter.id,
|
|
196
|
-
collection: resource.collection.name,
|
|
197
|
-
operation,
|
|
198
|
-
schema: resource.collection.schema ?? null,
|
|
199
|
-
source: 'managed-api',
|
|
200
|
-
},
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
export function getManagedApiOperations(
|
|
205
|
-
resource: ManagedApiGenerationResourceDefinition,
|
|
206
|
-
): readonly ManagedApiCrudOperation[] {
|
|
207
|
-
return resource.operations ?? MANAGED_API_CRUD_OPERATIONS;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
export function createManagedApiOperationId(
|
|
211
|
-
resourceName: string,
|
|
212
|
-
operation: ManagedApiCrudOperation,
|
|
213
|
-
): OperationId {
|
|
214
|
-
return `${resourceName}.${operation}`;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
export function createManagedApiResourceSchema(collection: DbCollectionDefinition): DataSchema {
|
|
218
|
-
const properties: Record<string, DataSchema> = {};
|
|
219
|
-
const required: string[] = [];
|
|
220
|
-
|
|
221
|
-
for (const field of collection.fields) {
|
|
222
|
-
properties[field.name] = createManagedApiFieldSchema(field);
|
|
223
|
-
if (field.required === true) required.push(field.name);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
return {
|
|
227
|
-
type: 'object',
|
|
228
|
-
title: collection.name,
|
|
229
|
-
required: required.length > 0 ? required : undefined,
|
|
230
|
-
properties,
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function createManagedApiResourceMetadata(
|
|
235
|
-
resource: ManagedApiGenerationResourceDefinition,
|
|
236
|
-
): DataContractValue | undefined {
|
|
237
|
-
if (resource.policies === undefined) return undefined;
|
|
238
|
-
|
|
239
|
-
return {
|
|
240
|
-
policies: resource.policies.map((policy) => {
|
|
241
|
-
const metadata: Record<string, DataContractValue> = {
|
|
242
|
-
id: policy.id,
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
if (policy.operation !== undefined) {
|
|
246
|
-
metadata.operation = policy.operation;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
return metadata;
|
|
250
|
-
}),
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
function createManagedApiOperationName(
|
|
255
|
-
resourceName: string,
|
|
256
|
-
operation: ManagedApiCrudOperation,
|
|
257
|
-
): string {
|
|
258
|
-
return `${operation} ${resourceName}`;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
function mapManagedApiOperationIntent(operation: ManagedApiCrudOperation): DataOperationIntent {
|
|
262
|
-
if (operation === 'list' || operation === 'read') return 'read';
|
|
263
|
-
if (operation === 'create') return 'create';
|
|
264
|
-
if (operation === 'update') return 'update';
|
|
265
|
-
return 'delete';
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
function createManagedApiOperationRequest(
|
|
269
|
-
collection: DbCollectionDefinition,
|
|
270
|
-
operation: ManagedApiCrudOperation,
|
|
271
|
-
primaryKeyParameter: DataOperationParameter | undefined,
|
|
272
|
-
): DataOperationConfig['request'] {
|
|
273
|
-
if (operation === 'list') {
|
|
274
|
-
return {
|
|
275
|
-
parameters: [
|
|
276
|
-
{
|
|
277
|
-
name: 'limit',
|
|
278
|
-
location: 'query',
|
|
279
|
-
schema: { type: 'integer' },
|
|
280
|
-
},
|
|
281
|
-
{
|
|
282
|
-
name: 'offset',
|
|
283
|
-
location: 'query',
|
|
284
|
-
schema: { type: 'integer' },
|
|
285
|
-
},
|
|
286
|
-
],
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
if (operation === 'read' || operation === 'delete') {
|
|
291
|
-
return primaryKeyParameter === undefined ? undefined : { parameters: [primaryKeyParameter] };
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
const parameters =
|
|
295
|
-
operation === 'update' && primaryKeyParameter !== undefined ? [primaryKeyParameter] : undefined;
|
|
296
|
-
|
|
297
|
-
return {
|
|
298
|
-
parameters,
|
|
299
|
-
schema: createManagedApiResourceSchema(collection),
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
function createManagedApiOperationResponse(
|
|
304
|
-
collection: DbCollectionDefinition,
|
|
305
|
-
operation: ManagedApiCrudOperation,
|
|
306
|
-
): DataOperationConfig['response'] {
|
|
307
|
-
const resourceSchema = createManagedApiResourceSchema(collection);
|
|
308
|
-
|
|
309
|
-
if (operation === 'list') {
|
|
310
|
-
return {
|
|
311
|
-
schema: {
|
|
312
|
-
type: 'array',
|
|
313
|
-
items: resourceSchema,
|
|
314
|
-
},
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
if (operation === 'delete') {
|
|
319
|
-
return {
|
|
320
|
-
schema: {
|
|
321
|
-
type: 'object',
|
|
322
|
-
properties: {
|
|
323
|
-
deleted: { type: 'boolean' },
|
|
324
|
-
},
|
|
325
|
-
},
|
|
326
|
-
};
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
return { schema: resourceSchema };
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
function createPrimaryKeyParameter(primaryKey: string): DataOperationParameter {
|
|
333
|
-
return {
|
|
334
|
-
name: primaryKey,
|
|
335
|
-
location: 'path',
|
|
336
|
-
required: true,
|
|
337
|
-
schema: {
|
|
338
|
-
type: 'string',
|
|
339
|
-
},
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
function resolveManagedApiPrimaryKey(collection: DbCollectionDefinition): string | undefined {
|
|
344
|
-
if (collection.primaryKey !== undefined) return collection.primaryKey;
|
|
345
|
-
return collection.fields.find((field) => field.unique === true)?.name;
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
function createManagedApiFieldSchema(field: DbFieldDefinition): DataSchema {
|
|
349
|
-
const schema: DataSchema = mapDbFieldTypeToDataSchema(field.type);
|
|
350
|
-
|
|
351
|
-
return {
|
|
352
|
-
...schema,
|
|
353
|
-
default: field.defaultValue,
|
|
354
|
-
};
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
function mapDbFieldTypeToDataSchema(type: DbFieldType): DataSchema {
|
|
358
|
-
if (type === 'boolean') return { type: 'boolean' };
|
|
359
|
-
if (type === 'datetime') return { type: 'string', format: 'date-time' };
|
|
360
|
-
if (type === 'json') return { type: 'object' };
|
|
361
|
-
if (type === 'number') return { type: 'number' };
|
|
362
|
-
if (type === 'uuid') return { type: 'string', format: 'uuid' };
|
|
363
|
-
return { type: 'string' };
|
|
364
|
-
}
|