@ankhorage/data-sources 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @ankhorage/data-sources
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - bc25acb: Add a headless endpoint test runner with dry-run request building, injected fetch execution, credential resolution, response parsing, and structured diagnostics.
8
+
9
+ ## 0.3.0
10
+
11
+ ### Minor Changes
12
+
13
+ - f4266e8: Add managed API generation helpers that derive normalized database endpoint and CRUD operation configs from provider-neutral adapter references and collection definitions.
14
+
3
15
  ## 0.2.0
4
16
 
5
17
  ### Minor Changes
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  # @ankhorage/data-sources
5
5
 
6
- ![license: MIT](././paradox/badges/license.svg) ![npm: v0.0.0](././paradox/badges/npm.svg) ![runtime: bun](././paradox/badges/runtime.svg) ![typescript: strict](././paradox/badges/typescript.svg) ![eslint: checked](././paradox/badges/eslint.svg) ![prettier: checked](././paradox/badges/prettier.svg) ![build: checked](././paradox/badges/build.svg) ![tests: checked](././paradox/badges/tests.svg) ![docs: paradox](././paradox/badges/docs.svg)
6
+ ![license: MIT](././paradox/badges/license.svg) ![npm: v0.3.0](././paradox/badges/npm.svg) ![runtime: bun](././paradox/badges/runtime.svg) ![typescript: strict](././paradox/badges/typescript.svg) ![eslint: checked](././paradox/badges/eslint.svg) ![prettier: checked](././paradox/badges/prettier.svg) ![build: checked](././paradox/badges/build.svg) ![tests: checked](././paradox/badges/tests.svg) ![docs: paradox](././paradox/badges/docs.svg)
7
7
 
8
8
  Provider-neutral data source and endpoint system for APIs, databases, and runtime bindings.
9
9
 
@@ -15,11 +15,13 @@ Provider-neutral data source and endpoint system for APIs, databases, and runtim
15
15
  - [Architecture overview](././paradox/diagrams/architecture-overview.mmd)
16
16
  - [Module relationships](././paradox/diagrams/module-relationships.mmd)
17
17
  - [Export graph](././paradox/diagrams/export-graph.mmd)
18
+ - [createManagedApiResourceSchema sequence](././paradox/diagrams/sequences/create-managed-api-resource-schema.mmd)
18
19
  - [createManualRestDataSource sequence](././paradox/diagrams/sequences/create-manual-rest-data-source.mmd)
19
20
  - [normalizeGraphQlIntrospectionOperations sequence](././paradox/diagrams/sequences/normalize-graph-ql-introspection-operations.mmd)
20
21
  - [normalizeGraphQlIntrospectionSchemas sequence](././paradox/diagrams/sequences/normalize-graph-ql-introspection-schemas.mmd)
21
22
  - [normalizeManualRestDataSource sequence](././paradox/diagrams/sequences/normalize-manual-rest-data-source.mmd)
22
23
  - [normalizeOpenApiSchema sequence](././paradox/diagrams/sequences/normalize-open-api-schema.mmd)
24
+ - [validateManagedApiDefinition sequence](././paradox/diagrams/sequences/validate-managed-api-definition.mmd)
23
25
  - [validateManualRestDataSource sequence](././paradox/diagrams/sequences/validate-manual-rest-data-source.mmd)
24
26
 
25
27
  ## Architecture preview
@@ -35,10 +37,14 @@ graph TD
35
37
  module_src_graphql_index_ts["src/graphql/index.ts"]
36
38
  package__ankhorage_data_sources -.-> module_src_graphql_index_ts
37
39
  module_src_index_ts["src/index.ts"]
40
+ module_src_managed_api_index_ts["src/managed-api/index.ts"]
41
+ package__ankhorage_data_sources -.-> module_src_managed_api_index_ts
38
42
  module_src_openapi_index_ts["src/openapi/index.ts"]
39
43
  package__ankhorage_data_sources -.-> module_src_openapi_index_ts
40
44
  module_src_rest_index_ts["src/rest/index.ts"]
41
45
  package__ankhorage_data_sources -.-> module_src_rest_index_ts
46
+ module_src_test_runner_index_ts["src/test-runner/index.ts"]
47
+ package__ankhorage_data_sources -.-> module_src_test_runner_index_ts
42
48
  ```
43
49
 
44
50
  </details>
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import type { DataSourceConfig, DataSourceKind } from '@ankhorage/contracts/data';
2
2
  export * from './graphql';
3
+ export * from './managed-api';
3
4
  export * from './openapi';
4
5
  export * from './rest';
6
+ export * from './test-runner';
5
7
  export declare const DATA_SOURCES_PACKAGE_NAME: "@ankhorage/data-sources";
6
8
  export declare const SUPPORTED_DATA_SOURCE_KINDS: readonly ["database", "graphql", "managed-api", "openapi", "rest"];
7
9
  export interface DataSourcesPackageInfo {
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAElF,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC;AAEvB,eAAO,MAAM,yBAAyB,EAAG,yBAAkC,CAAC;AAE5E,eAAO,MAAM,2BAA2B,oEAMM,CAAC;AAE/C,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,WAAW,EAAE,OAAO,yBAAyB,CAAC;IACvD,QAAQ,CAAC,cAAc,EAAE,OAAO,2BAA2B,CAAC;CAC7D;AAED,wBAAgB,yBAAyB,IAAI,sBAAsB,CAKlE;AAED,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,IAAI,cAAc,CAE9E;AAED,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,gBAAgB,GAAG,cAAc,CAE1E"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAElF,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC;AACvB,cAAc,eAAe,CAAC;AAE9B,eAAO,MAAM,yBAAyB,EAAG,yBAAkC,CAAC;AAE5E,eAAO,MAAM,2BAA2B,oEAMM,CAAC;AAE/C,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,WAAW,EAAE,OAAO,yBAAyB,CAAC;IACvD,QAAQ,CAAC,cAAc,EAAE,OAAO,2BAA2B,CAAC;CAC7D;AAED,wBAAgB,yBAAyB,IAAI,sBAAsB,CAKlE;AAED,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,IAAI,cAAc,CAE9E;AAED,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,gBAAgB,GAAG,cAAc,CAE1E"}
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  export * from './graphql';
2
+ export * from './managed-api';
2
3
  export * from './openapi';
3
4
  export * from './rest';
5
+ export * from './test-runner';
4
6
  export const DATA_SOURCES_PACKAGE_NAME = '@ankhorage/data-sources';
5
7
  export const SUPPORTED_DATA_SOURCE_KINDS = [
6
8
  'database',
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC;AAEvB,MAAM,CAAC,MAAM,yBAAyB,GAAG,yBAAkC,CAAC;AAE5E,MAAM,CAAC,MAAM,2BAA2B,GAAG;IACzC,UAAU;IACV,SAAS;IACT,aAAa;IACb,SAAS;IACT,MAAM;CACsC,CAAC;AAO/C,MAAM,UAAU,yBAAyB;IACvC,OAAO;QACL,WAAW,EAAE,yBAAyB;QACtC,cAAc,EAAE,2BAA2B;KAC5C,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,yBAAyB,CAAC,IAAY;IACpD,OAAO,2BAA2B,CAAC,IAAI,CAAC,CAAC,aAAa,EAAE,EAAE,CAAC,aAAa,KAAK,IAAI,CAAC,CAAC;AACrF,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,MAAwB;IACxD,OAAO,MAAM,CAAC,IAAI,CAAC;AACrB,CAAC","sourcesContent":["import type { DataSourceConfig, DataSourceKind } from '@ankhorage/contracts/data';\n\nexport * from './graphql';\nexport * from './openapi';\nexport * from './rest';\n\nexport const DATA_SOURCES_PACKAGE_NAME = '@ankhorage/data-sources' as const;\n\nexport const SUPPORTED_DATA_SOURCE_KINDS = [\n 'database',\n 'graphql',\n 'managed-api',\n 'openapi',\n 'rest',\n] as const satisfies readonly DataSourceKind[];\n\nexport interface DataSourcesPackageInfo {\n readonly packageName: typeof DATA_SOURCES_PACKAGE_NAME;\n readonly supportedKinds: typeof SUPPORTED_DATA_SOURCE_KINDS;\n}\n\nexport function getDataSourcesPackageInfo(): DataSourcesPackageInfo {\n return {\n packageName: DATA_SOURCES_PACKAGE_NAME,\n supportedKinds: SUPPORTED_DATA_SOURCE_KINDS,\n };\n}\n\nexport function isSupportedDataSourceKind(kind: string): kind is DataSourceKind {\n return SUPPORTED_DATA_SOURCE_KINDS.some((supportedKind) => supportedKind === kind);\n}\n\nexport function getDataSourceKind(source: DataSourceConfig): DataSourceKind {\n return source.kind;\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC;AACvB,cAAc,eAAe,CAAC;AAE9B,MAAM,CAAC,MAAM,yBAAyB,GAAG,yBAAkC,CAAC;AAE5E,MAAM,CAAC,MAAM,2BAA2B,GAAG;IACzC,UAAU;IACV,SAAS;IACT,aAAa;IACb,SAAS;IACT,MAAM;CACsC,CAAC;AAO/C,MAAM,UAAU,yBAAyB;IACvC,OAAO;QACL,WAAW,EAAE,yBAAyB;QACtC,cAAc,EAAE,2BAA2B;KAC5C,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,yBAAyB,CAAC,IAAY;IACpD,OAAO,2BAA2B,CAAC,IAAI,CAAC,CAAC,aAAa,EAAE,EAAE,CAAC,aAAa,KAAK,IAAI,CAAC,CAAC;AACrF,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,MAAwB;IACxD,OAAO,MAAM,CAAC,IAAI,CAAC;AACrB,CAAC","sourcesContent":["import type { DataSourceConfig, DataSourceKind } from '@ankhorage/contracts/data';\n\nexport * from './graphql';\nexport * from './managed-api';\nexport * from './openapi';\nexport * from './rest';\nexport * from './test-runner';\n\nexport const DATA_SOURCES_PACKAGE_NAME = '@ankhorage/data-sources' as const;\n\nexport const SUPPORTED_DATA_SOURCE_KINDS = [\n 'database',\n 'graphql',\n 'managed-api',\n 'openapi',\n 'rest',\n] as const satisfies readonly DataSourceKind[];\n\nexport interface DataSourcesPackageInfo {\n readonly packageName: typeof DATA_SOURCES_PACKAGE_NAME;\n readonly supportedKinds: typeof SUPPORTED_DATA_SOURCE_KINDS;\n}\n\nexport function getDataSourcesPackageInfo(): DataSourcesPackageInfo {\n return {\n packageName: DATA_SOURCES_PACKAGE_NAME,\n supportedKinds: SUPPORTED_DATA_SOURCE_KINDS,\n };\n}\n\nexport function isSupportedDataSourceKind(kind: string): kind is DataSourceKind {\n return SUPPORTED_DATA_SOURCE_KINDS.some((supportedKind) => supportedKind === kind);\n}\n\nexport function getDataSourceKind(source: DataSourceConfig): DataSourceKind {\n return source.kind;\n}\n"]}
@@ -0,0 +1,30 @@
1
+ import type { AdapterRef, DataEndpointConfig, DataOperationConfig, DataSchema, DataSourceDiagnostic, DataSourceDiagnosticResult, ManagedApiDataSourceConfig, OperationId } from '@ankhorage/contracts/data';
2
+ import type { DbCollectionDefinition } from '@ankhorage/contracts/db';
3
+ export declare const MANAGED_API_CRUD_OPERATIONS: readonly ["list", "read", "create", "update", "delete"];
4
+ export type ManagedApiCrudOperation = (typeof MANAGED_API_CRUD_OPERATIONS)[number];
5
+ export interface ManagedApiOperationPolicyRef {
6
+ readonly id: string;
7
+ readonly operation?: string;
8
+ }
9
+ export interface ManagedApiGenerationResourceDefinition {
10
+ readonly name: string;
11
+ readonly collection: DbCollectionDefinition;
12
+ readonly operations?: readonly ManagedApiCrudOperation[];
13
+ readonly policies?: readonly ManagedApiOperationPolicyRef[];
14
+ }
15
+ export interface ManagedApiGenerationDefinition {
16
+ readonly id: string;
17
+ readonly adapter: AdapterRef;
18
+ readonly name?: string;
19
+ readonly description?: string;
20
+ readonly resources: readonly ManagedApiGenerationResourceDefinition[];
21
+ }
22
+ export declare function createManagedApiDataSource(definition: ManagedApiGenerationDefinition): DataSourceDiagnosticResult<ManagedApiDataSourceConfig>;
23
+ export declare function validateManagedApiDefinition(definition: ManagedApiGenerationDefinition): readonly DataSourceDiagnostic[];
24
+ export declare function normalizeManagedApiDataSource(definition: ManagedApiGenerationDefinition): ManagedApiDataSourceConfig;
25
+ export declare function createManagedApiEndpoint(definition: ManagedApiGenerationDefinition, resource: ManagedApiGenerationResourceDefinition, operations?: readonly ManagedApiCrudOperation[]): DataEndpointConfig;
26
+ export declare function createManagedApiOperation(definition: ManagedApiGenerationDefinition, resource: ManagedApiGenerationResourceDefinition, operation: ManagedApiCrudOperation): DataOperationConfig;
27
+ export declare function getManagedApiOperations(resource: ManagedApiGenerationResourceDefinition): readonly ManagedApiCrudOperation[];
28
+ export declare function createManagedApiOperationId(resourceName: string, operation: ManagedApiCrudOperation): OperationId;
29
+ export declare function createManagedApiResourceSchema(collection: DbCollectionDefinition): DataSchema;
30
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/managed-api/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EAEV,kBAAkB,EAClB,mBAAmB,EAGnB,UAAU,EACV,oBAAoB,EACpB,0BAA0B,EAC1B,0BAA0B,EAE1B,WAAW,EACZ,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EACV,sBAAsB,EAGvB,MAAM,yBAAyB,CAAC;AAEjC,eAAO,MAAM,2BAA2B,yDAA0D,CAAC;AAEnG,MAAM,MAAM,uBAAuB,GAAG,CAAC,OAAO,2BAA2B,CAAC,CAAC,MAAM,CAAC,CAAC;AAE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AAuB,EAAE,CAEpC;AAED,wBAAgB,2BAA2B,CACzC,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,uBAAuB,GACjC,WAAW,CAEb;AAED,wBAAgB,8BAA8B,CAAC,UAAU,EAAE,sBAAsB,GAAG,UAAU,CAe7F"}
@@ -0,0 +1,257 @@
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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/managed-api/index.ts"],"names":[],"mappings":"AAoBA,MAA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AQ;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"]}
@@ -0,0 +1,60 @@
1
+ import type { CredentialRef, DataContractValue, DataSourceConfig, DataSourceDiagnostic, EndpointId, OperationId } from '@ankhorage/contracts/data';
2
+ export type EndpointTestInputValues = Readonly<Record<string, DataContractValue>>;
3
+ export type EndpointTestHeaders = Readonly<Record<string, string>>;
4
+ export interface EndpointTestCredential {
5
+ readonly headers?: EndpointTestHeaders;
6
+ readonly query?: EndpointTestInputValues;
7
+ }
8
+ export type EndpointTestCredentialResolver = (credential: CredentialRef) => EndpointTestCredential | Promise<EndpointTestCredential | undefined> | undefined;
9
+ export interface EndpointTestFetchInit {
10
+ readonly method: string;
11
+ readonly headers: EndpointTestHeaders;
12
+ readonly body?: string;
13
+ }
14
+ export interface EndpointTestFetchResponse {
15
+ readonly status: number;
16
+ readonly headers?: EndpointTestHeaders;
17
+ text(): Promise<string>;
18
+ }
19
+ export type EndpointTestFetch = (url: string, init: EndpointTestFetchInit) => Promise<EndpointTestFetchResponse>;
20
+ export interface EndpointTestInput {
21
+ readonly dataSource: DataSourceConfig;
22
+ readonly endpointId: EndpointId;
23
+ readonly operationId: OperationId;
24
+ readonly values?: EndpointTestInputValues;
25
+ readonly dryRun?: boolean;
26
+ readonly fetch?: EndpointTestFetch;
27
+ readonly credentialResolver?: EndpointTestCredentialResolver;
28
+ }
29
+ export interface EndpointTestRequestDiagnostic {
30
+ readonly dataSourceId: string;
31
+ readonly endpointId: EndpointId;
32
+ readonly operationId: OperationId;
33
+ readonly url: string;
34
+ readonly method: string;
35
+ readonly headers: EndpointTestHeaders;
36
+ readonly body?: string;
37
+ readonly dryRun: boolean;
38
+ }
39
+ export interface EndpointTestResponseDiagnostic {
40
+ readonly status: number;
41
+ readonly ok: boolean;
42
+ readonly headers?: EndpointTestHeaders;
43
+ readonly bodyText?: string;
44
+ readonly parsedBody?: DataContractValue;
45
+ }
46
+ export type EndpointTestResult = {
47
+ readonly ok: true;
48
+ readonly request: EndpointTestRequestDiagnostic;
49
+ readonly response?: EndpointTestResponseDiagnostic;
50
+ readonly data?: DataContractValue;
51
+ readonly diagnostics: readonly DataSourceDiagnostic[];
52
+ } | {
53
+ readonly ok: false;
54
+ readonly request?: EndpointTestRequestDiagnostic;
55
+ readonly response?: EndpointTestResponseDiagnostic;
56
+ readonly diagnostics: readonly DataSourceDiagnostic[];
57
+ };
58
+ export declare function testEndpoint(input: EndpointTestInput): Promise<EndpointTestResult>;
59
+ export declare function buildEndpointTestRequest(input: EndpointTestInput): Promise<EndpointTestResult>;
60
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/test-runner/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,iBAAiB,EAIjB,gBAAgB,EAChB,oBAAoB,EACpB,UAAU,EACV,WAAW,EACZ,MAAM,2BAA2B,CAAC;AAEnC,MAAM,MAAM,uBAAuB,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC,CAAC;AAClF,MAAM,MAAM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