Skip to content

Commit

Permalink
Fix generation of resources
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Aug 29, 2024
1 parent cdd10b1 commit 08fe093
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 34 deletions.
22 changes: 15 additions & 7 deletions generate/extractInfoFromSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function findTypeInDataProperty(schema: JsonRefParser.JSONSchema) {
return findTypeInDataObjects(findDataObjects(schema));
}

function findRequiredIdInDataObject(dataSchema: JsonRefParser.JSONSchema) {
function findIdIsRequiredInDataObject(dataSchema: JsonRefParser.JSONSchema) {
if (dataSchema.type !== 'object') {
throw new Error('Data not an object?');
}
Expand All @@ -211,22 +211,22 @@ function findRequiredIdInDataObject(dataSchema: JsonRefParser.JSONSchema) {
return idIsRequired || undefined;
}

function findIdInDataProperty(schema: JsonRefParser.JSONSchema) {
function findIdIsRequired(schema: JsonRefParser.JSONSchema) {
if (typeof schema.properties?.data !== 'object') {
throw new Error('Missing data!');
}

const dataSchema = schema.properties.data;

if (dataSchema.type === 'array') {
return undefined;
return true;
}

if (dataSchema.anyOf) {
const types = [
...new Set(
dataSchema.anyOf.map((s) =>
findRequiredIdInDataObject(s as JsonRefParser.JSONSchema),
findIdIsRequiredInDataObject(s as JsonRefParser.JSONSchema),
),
),
];
Expand All @@ -236,7 +236,7 @@ function findIdInDataProperty(schema: JsonRefParser.JSONSchema) {
return types[0];
}

return findRequiredIdInDataObject(dataSchema);
return findIdIsRequiredInDataObject(dataSchema);
}

function findPropertiesInDataProperty(
Expand All @@ -247,8 +247,16 @@ function findPropertiesInDataProperty(
return [];
}

const datas = findDataObjects(schema);

if (datas.length > 1) {
return '*';
}

const data = datas[0]!;

try {
return findPropertiesInProperty(schema.properties.data, property);
return findPropertiesInProperty(data, property);
} catch (e) {
if (e instanceof NoPropertiesDefinedError) {
return '*';
Expand Down Expand Up @@ -357,7 +365,7 @@ function generateResourceInfo(
requestStructure: link.schema
? {
type: findTypeInDataProperty(link.schema),
idRequired: findIdInDataProperty(link.schema),
idRequired: findIdIsRequired(link.schema),
attributes: findPropertiesInDataProperty(link.schema, 'attributes'),
relationships: findPropertiesInDataProperty(
link.schema,
Expand Down
11 changes: 10 additions & 1 deletion packages/cma-client-node/__tests__/menuItem.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { generateNewCmaClient } from '../../../jest-helpers/generateNewCmaClient';
import { generateId } from '../../cma-client/src';
import { LogLevel } from '../src';

describe('menu item', () => {
it.concurrent('create, find, list, update, destroy', async () => {
Expand All @@ -27,6 +26,16 @@ describe('menu item', () => {

await client.menuItems.destroy(menuItem);
expect(await client.menuItems.list()).toHaveLength(0);

const menuItem2 = await client.menuItems.create({
label: 'Other',
position: 2,
});

await client.menuItems.reorder([
{ id: menuItem2.id, position: 2, parent: null },
{ id: menuItem2.id, position: 1, parent: null },
]);
});

it.concurrent('create with explicit ID', async () => {
Expand Down
24 changes: 14 additions & 10 deletions packages/cma-client/resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,9 @@
"optionalRequestBody": false,
"requestStructure": {
"type": "menu_item",
"attributes": [],
"relationships": []
"idRequired": true,
"attributes": ["position"],
"relationships": ["parent"]
},
"queryParamsRequired": false,
"responseType": "MenuItemReorderJobSchema",
Expand Down Expand Up @@ -686,8 +687,9 @@
"optionalRequestBody": false,
"requestStructure": {
"type": "schema_menu_item",
"attributes": [],
"relationships": []
"idRequired": true,
"attributes": ["position"],
"relationships": ["parent"]
},
"queryParamsRequired": false,
"responseType": "SchemaMenuItemReorderJobSchema",
Expand Down Expand Up @@ -831,8 +833,9 @@
"optionalRequestBody": false,
"requestStructure": {
"type": "upload_collection",
"attributes": [],
"relationships": []
"idRequired": true,
"attributes": ["position"],
"relationships": ["parent"]
},
"queryParamsRequired": false,
"responseType": "UploadCollectionReorderJobSchema",
Expand Down Expand Up @@ -1045,8 +1048,9 @@
"optionalRequestBody": false,
"requestStructure": {
"type": "*",
"attributes": [],
"relationships": []
"idRequired": true,
"attributes": "*",
"relationships": "*"
},
"queryParamsRequired": false,
"responseType": "ItemTypeReorderFieldsAndFieldsetsJobSchema",
Expand Down Expand Up @@ -4103,8 +4107,8 @@
"optionalRequestBody": false,
"requestStructure": {
"type": "*",
"attributes": [],
"relationships": ["item", "item", "item"]
"attributes": "*",
"relationships": "*"
},
"queryParamsRequired": false,
"responseType": "EditingSessionUpdateTargetSchema",
Expand Down
4 changes: 2 additions & 2 deletions packages/cma-client/src/generated/resources/MenuItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ export default class MenuItem extends BaseResource {
return this.rawReorder(
Utils.serializeRequestBody<SchemaTypes.MenuItemReorderSchema>(body, {
type: 'menu_item',
attributes: [],
relationships: [],
attributes: ['position'],
relationships: ['parent'],
}),
).then((body) =>
Utils.deserializeResponseBody<SimpleSchemaTypes.MenuItemReorderJobSchema>(
Expand Down
4 changes: 2 additions & 2 deletions packages/cma-client/src/generated/resources/SchemaMenuItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ export default class SchemaMenuItem extends BaseResource {
body,
{
type: 'schema_menu_item',
attributes: [],
relationships: [],
attributes: ['position'],
relationships: ['parent'],
},
),
).then((body) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ export default class UploadCollection extends BaseResource {
body,
{
type: 'upload_collection',
attributes: [],
relationships: [],
attributes: ['position'],
relationships: ['parent'],
},
),
).then((body) =>
Expand Down
12 changes: 2 additions & 10 deletions packages/dashboard-client/resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,8 @@
"optionalRequestBody": false,
"requestStructure": {
"type": "*",
"attributes": [
"email",
"password",
"otp_code",
"token",
"account_id",
"password",
"otp_code"
],
"relationships": []
"attributes": "*",
"relationships": "*"
},
"queryParamsRequired": false,
"responseType": "SessionCreateTargetSchema",
Expand Down

0 comments on commit 08fe093

Please sign in to comment.