@jskit-ai/json-rest-api-core 0.1.1 → 0.1.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.
@@ -1,7 +1,7 @@
1
1
  export default Object.freeze({
2
2
  packageVersion: 1,
3
3
  packageId: "@jskit-ai/json-rest-api-core",
4
- version: "0.1.1",
4
+ version: "0.1.3",
5
5
  kind: "runtime",
6
6
  description: "Shared internal json-rest-api host runtime for JSKIT server packages.",
7
7
  dependsOn: [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/json-rest-api-core",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "node --test"
@@ -11,6 +11,6 @@
11
11
  "dependencies": {
12
12
  "hooked-api": "1.x.x",
13
13
  "json-rest-api": "1.x.x",
14
- "@jskit-ai/kernel": "0.1.56"
14
+ "@jskit-ai/kernel": "0.1.58"
15
15
  }
16
16
  }
@@ -63,6 +63,9 @@ function cloneJsonRestResourceValue(value, { writeSerializers = {} } = {}) {
63
63
 
64
64
  if (isPlainJsonRestObject(next.storage)) {
65
65
  const serializerKey = normalizeJsonRestText(next.storage.writeSerializer).toLowerCase();
66
+ if (next.storage.virtual === true) {
67
+ next.virtual = true;
68
+ }
66
69
  if (serializerKey) {
67
70
  const serializer = writeSerializers[serializerKey];
68
71
  if (typeof serializer !== "function") {
@@ -195,16 +198,68 @@ function buildJsonRestQueryParams(resourceType = "", query = {}, { include = und
195
198
  return queryParams;
196
199
  }
197
200
 
198
- function createJsonApiInputRecord(resourceType = "", attributes = {}, { id = null, relationships = null } = {}) {
199
- const normalizedRelationships = normalizeJsonRestObject(relationships);
201
+ function extractJsonApiInputRelationships(attributes = {}, resource = null, relationships = null) {
202
+ const normalizedAttributes = {
203
+ ...normalizeJsonRestObject(attributes)
204
+ };
205
+ const normalizedRelationships = {
206
+ ...normalizeJsonRestObject(relationships)
207
+ };
208
+ const resourceSchema = normalizeJsonRestObject(resource?.schema);
209
+
210
+ for (const [fieldName, fieldDefinition] of Object.entries(resourceSchema)) {
211
+ if (!Object.hasOwn(normalizedAttributes, fieldName)) {
212
+ continue;
213
+ }
214
+
215
+ const normalizedFieldDefinition = normalizeJsonRestObject(fieldDefinition);
216
+ const relationshipType = normalizeJsonRestText(normalizedFieldDefinition.belongsTo);
217
+ if (!relationshipType) {
218
+ continue;
219
+ }
220
+
221
+ const relationshipName = normalizeJsonRestText(normalizedFieldDefinition.as, {
222
+ fallback: fieldName
223
+ });
224
+ if (!relationshipName) {
225
+ continue;
226
+ }
227
+
228
+ const relationshipValue = normalizedAttributes[fieldName];
229
+ delete normalizedAttributes[fieldName];
230
+
231
+ if (relationshipValue === undefined) {
232
+ continue;
233
+ }
234
+
235
+ if (!Object.hasOwn(normalizedRelationships, relationshipName)) {
236
+ normalizedRelationships[relationshipName] = createJsonApiRelationship(
237
+ relationshipType,
238
+ relationshipValue
239
+ );
240
+ }
241
+ }
242
+
243
+ return {
244
+ attributes: normalizedAttributes,
245
+ relationships: normalizedRelationships
246
+ };
247
+ }
248
+
249
+ function createJsonApiInputRecord(
250
+ resourceType = "",
251
+ attributes = {},
252
+ { id = null, relationships = null, resource = null } = {}
253
+ ) {
254
+ const normalizedInput = extractJsonApiInputRelationships(attributes, resource, relationships);
200
255
  return {
201
256
  data: {
202
257
  type: normalizeJsonRestText(resourceType),
203
258
  ...(id == null ? {} : { id: String(id) }),
204
- attributes: {
205
- ...normalizeJsonRestObject(attributes)
206
- },
207
- ...(Object.keys(normalizedRelationships).length < 1 ? {} : { relationships: normalizedRelationships })
259
+ attributes: normalizedInput.attributes,
260
+ ...(Object.keys(normalizedInput.relationships).length < 1
261
+ ? {}
262
+ : { relationships: normalizedInput.relationships })
208
263
  }
209
264
  };
210
265
  }
@@ -106,6 +106,9 @@ test("createJsonRestApiHost installs normalizeRecordId as the default resource i
106
106
  });
107
107
 
108
108
  assert.equal(api.vars.normalizeId, normalizeRecordId);
109
+ assert.equal(api.vars.normalizeId(7), "7");
110
+ assert.equal(api.vars.normalizeId(0), null);
111
+ assert.equal(api.vars.normalizeId(7.5), null);
109
112
  });
110
113
 
111
114
  test("shared query/document helpers build json-rest-api request shapes", () => {
@@ -162,6 +165,42 @@ test("shared query/document helpers build json-rest-api request shapes", () => {
162
165
  }
163
166
  );
164
167
 
168
+ assert.deepEqual(
169
+ createJsonApiInputRecord("products", {
170
+ serviceId: "9",
171
+ name: "Style Groom"
172
+ }, {
173
+ resource: {
174
+ schema: {
175
+ serviceId: {
176
+ type: "id",
177
+ belongsTo: "services",
178
+ as: "service"
179
+ },
180
+ name: {
181
+ type: "string"
182
+ }
183
+ }
184
+ }
185
+ }),
186
+ {
187
+ data: {
188
+ type: "products",
189
+ attributes: {
190
+ name: "Style Groom"
191
+ },
192
+ relationships: {
193
+ service: {
194
+ data: {
195
+ type: "services",
196
+ id: "9"
197
+ }
198
+ }
199
+ }
200
+ }
201
+ }
202
+ );
203
+
165
204
  assert.deepEqual(
166
205
  simplifyJsonApiDocument({
167
206
  data: [
@@ -234,6 +273,17 @@ test("createJsonRestResourceScopeOptions clones canonical resource metadata and
234
273
  required: true
235
274
  })
236
275
  })
276
+ }),
277
+ bookingSteps: Object.freeze({
278
+ type: "array",
279
+ storage: Object.freeze({
280
+ virtual: true
281
+ }),
282
+ operations: Object.freeze({
283
+ output: Object.freeze({
284
+ required: false
285
+ })
286
+ })
237
287
  })
238
288
  }),
239
289
  operations: Object.freeze({
@@ -255,7 +305,9 @@ test("createJsonRestResourceScopeOptions clones canonical resource metadata and
255
305
  assert.notEqual(result.schema.createdAt, source.schema.createdAt);
256
306
  assert.equal(result.schema.createdAt.storage.column, "created_at");
257
307
  assert.equal(result.schema.createdAt.storage.serialize, serializer);
308
+ assert.equal(result.schema.createdAt.storage.serialize(null), null);
258
309
  assert.equal(result.schema.createdAt.storage.writeSerializer, undefined);
310
+ assert.equal(result.schema.bookingSteps.virtual, true);
259
311
  assert.equal(result.normalizeId, normalizeId);
260
312
  assert.equal(result.schema.name.maxLength, 190);
261
313
  assert.equal(result.schema.name.operations.output.required, true);