diff --git a/generate/extractInfoFromSchema.ts b/generate/extractInfoFromSchema.ts index 73da573a..94bc798c 100644 --- a/generate/extractInfoFromSchema.ts +++ b/generate/extractInfoFromSchema.ts @@ -1,4 +1,6 @@ -import JsonRefParser, { JSONSchema } from '@apidevtools/json-schema-ref-parser'; +import JsonRefParser, { + type JSONSchema, +} from '@apidevtools/json-schema-ref-parser'; import fetch from 'cross-fetch'; import { compile as hyperschemaToTypings } from 'hyperschema-to-ts'; import simplifySchema from './generateSimplifiedSchema'; diff --git a/generate/generateSimplifiedSchema.ts b/generate/generateSimplifiedSchema.ts index 2dc69286..92c0692e 100644 --- a/generate/generateSimplifiedSchema.ts +++ b/generate/generateSimplifiedSchema.ts @@ -1,12 +1,10 @@ -import JsonRefParser from '@apidevtools/json-schema-ref-parser'; - function simplifySchema(objectSchema: any) { const { attributes, relationships, meta, id, type } = objectSchema.properties as any; return { ...objectSchema, - additionalProperties: attributes && !attributes.properties, + additionalProperties: Boolean(attributes && !attributes.properties), required: [ ...(objectSchema.required?.includes('attributes') ? attributes?.required || [] @@ -15,6 +13,9 @@ function simplifySchema(objectSchema: any) { ? relationships?.required || [] : []), ...(objectSchema.required?.includes('meta') ? ['meta'] : []), + ...(!attributes && !relationships && !meta + ? objectSchema.required.filter((attr: string) => attr !== 'type') + : []), ], properties: { ...(id ? { id } : {}), @@ -62,25 +63,23 @@ function simplifyEntity(objectSchema: any) { ...(id ? { id } : {}), ...(type ? { type } : {}), ...(attributes?.properties - ? Object.keys(attributes.properties).reduce( - (acc, key) => ({ - ...acc, - [key]: { + ? Object.fromEntries( + Object.keys(attributes.properties).map((key) => [ + key, + { $ref: `${objectSchema.properties.attributes.$ref}/properties/${key}`, }, - }), - {}, + ]), ) : {}), ...(relationships?.properties - ? Object.keys(relationships.properties).reduce( - (acc, key) => ({ - ...acc, - [key]: { + ? Object.fromEntries( + Object.keys(relationships.properties).map((key) => [ + key, + { $ref: `${objectSchema.properties.relationships.$ref}/properties/${key}`, }, - }), - {}, + ]), ) : {}), ...(meta ? { meta } : {}), @@ -97,12 +96,12 @@ function simplifyEntityRelationships(schema: any) { } if (schema.definitions.relationships) { - Object.entries(schema.definitions.relationships.properties).forEach( - ([rel, relSchema]) => { - schema.definitions.relationships.properties[rel] = - schema.definitions.relationships.properties[rel].properties.data; - }, - ); + for (const rel of Object.keys( + schema.definitions.relationships.properties, + )) { + schema.definitions.relationships.properties[rel] = + schema.definitions.relationships.properties[rel].properties.data; + } } } @@ -150,10 +149,13 @@ function simplifyTargetSchema(schema: any) { } export default function simplifyLinks(schema: any) { - Object.entries(schema.definitions).forEach(([jsonApiType, schema]) => { - simplifyEntityRelationships(schema); - if (schema.links) { - schema.links.forEach((link: any) => { + for (const [jsonApiType, subschema] of Object.entries( + schema.definitions, + )) { + simplifyEntityRelationships(subschema); + + if (subschema.links) { + for (const link of subschema.links) { const originalSchema = link.schema; link.schema = applyToInnerObject( @@ -180,7 +182,7 @@ export default function simplifyLinks(schema: any) { link.targetSchema?.properties?.data, ); link.jobSchema = simplifyTargetSchema(link.jobSchema?.properties?.data); - }); + } } - }); + } } diff --git a/generate/index.ts b/generate/index.ts index d0f063de..90a74b70 100755 --- a/generate/index.ts +++ b/generate/index.ts @@ -1,11 +1,11 @@ #!/usr/bin/env node --stack_size=800 -r ts-node/register -import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'fs'; +import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; import Handlebars from 'handlebars'; import rimraf from 'rimraf'; import extractInfoFromSchema, { - ResourceInfo, - SchemaInfo, + type ResourceInfo, + type SchemaInfo, } from './extractInfoFromSchema'; import toSafeName from './toSafeName'; @@ -49,7 +49,7 @@ async function generate(prefix: string, hyperschemaUrl: string) { writeFileSync( `./packages/${prefix}-client/resources.json`, - JSON.stringify( + `${JSON.stringify( other.resources.map((r) => ({ ...r, endpoints: r.endpoints.map( @@ -61,7 +61,7 @@ async function generate(prefix: string, hyperschemaUrl: string) { })), null, 2, - ) + '\n', + )}\n`, { encoding: 'utf-8' }, ); diff --git a/generate/setClientVersion.ts b/generate/setClientVersion.ts index a5c06a91..0529f327 100755 --- a/generate/setClientVersion.ts +++ b/generate/setClientVersion.ts @@ -1,8 +1,8 @@ #!/usr/bin/env node -r ts-node/register -import { readFileSync, writeFileSync } from 'fs'; +import { readFileSync, writeFileSync } from 'node:fs'; -['cma-client', 'dashboard-client'].forEach((dir) => { +for (const dir of ['cma-client', 'dashboard-client']) { const version: string = JSON.parse( readFileSync(`./packages/${dir}/package.json`, 'utf8'), ).version; @@ -16,4 +16,4 @@ import { readFileSync, writeFileSync } from 'fs'; sourceFile.replace(`@datocms/${dir}`, `@datocms/${dir} v${version}`), 'utf-8', ); -}); +} diff --git a/package.json b/package.json index c101e5fe..1e043bd3 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "scripts": { "prepare": "husky install", "lint": "biome ci packages", - "format": "biome check packages --apply-unsafe && biome format --write packages", + "format": "biome check generate packages --apply-unsafe && biome format --write generate packages", "test": "npm run lint && jest --maxConcurrency=5", "test-next": "ACCOUNT_API_BASE_URL=http://account-api.lvh.me:3001 SITE_API_BASE_URL=http://site-api.lvh.me:3001 PUSHER_APP_KEY=12a5ddac68784be0fc59 PUSHER_CLUSTER=eu npm run test", "generate": "./generate/index.ts && npm run format", diff --git a/packages/cma-client-node/__tests__/environment.test.ts b/packages/cma-client-node/__tests__/environment.test.ts index 7255b2bb..dc75e313 100644 --- a/packages/cma-client-node/__tests__/environment.test.ts +++ b/packages/cma-client-node/__tests__/environment.test.ts @@ -12,11 +12,15 @@ describe('environments', () => { }, ); - await client.environments.promote(forkedEnvironment.id); + const renamedSandbox = await client.environments.rename(forkedEnvironment, { + id: 'renamed-sandbox', + }); + + await client.environments.promote(renamedSandbox); await client.environments.promote(primaryEnvironment.id); - await client.environments.destroy(forkedEnvironment.id); + await client.environments.destroy(renamedSandbox); const environments = await client.environments.list(); expect(environments.length).toEqual(1); diff --git a/packages/cma-client/resources.json b/packages/cma-client/resources.json index acdf1a3d..bb42510b 100644 --- a/packages/cma-client/resources.json +++ b/packages/cma-client/resources.json @@ -2295,6 +2295,31 @@ "name": "promote", "rawName": "rawPromote" }, + { + "returnsCollection": false, + "docUrl": "https://www.datocms.com/docs/content-management-api/resources/environment/rename", + "rel": "rename", + "urlTemplate": "/environments/${environmentId}/rename", + "method": "PUT", + "comment": "Rename an environment", + "urlPlaceholder": { + "variableName": "environmentId", + "isEntityId": true, + "relType": "EnvironmentData" + }, + "requestBodyType": "EnvironmentRenameSchema", + "optionalRequestBody": false, + "requestStructure": { + "type": "environment", + "idRequired": true, + "attributes": [], + "relationships": [] + }, + "queryParamsRequired": false, + "responseType": "EnvironmentRenameTargetSchema", + "name": "rename", + "rawName": "rawRename" + }, { "returnsCollection": true, "docUrl": "https://www.datocms.com/docs/content-management-api/resources/environment/instances", diff --git a/packages/cma-client/src/generated/Client.ts b/packages/cma-client/src/generated/Client.ts index 068802b7..4d9b6848 100644 --- a/packages/cma-client/src/generated/Client.ts +++ b/packages/cma-client/src/generated/Client.ts @@ -143,7 +143,7 @@ export class Client { ...this.config, ...options, logFn: this.config.logFn || console.log, - userAgent: '@datocms/cma-client v3.1.8', + userAgent: '@datocms/cma-client', baseUrl: this.baseUrl, preCallStack: new Error().stack, extraHeaders: { diff --git a/packages/cma-client/src/generated/SchemaTypes.ts b/packages/cma-client/src/generated/SchemaTypes.ts index d984a31b..689d902e 100644 --- a/packages/cma-client/src/generated/SchemaTypes.ts +++ b/packages/cma-client/src/generated/SchemaTypes.ts @@ -8490,6 +8490,28 @@ export type EnvironmentPromoteTargetSchema = { data: Environment; }; +/** + * This interface was referenced by `Environment`'s JSON-Schema + * via the `rename.schema` link. + */ +export type EnvironmentRenameSchema = { + data: { + type: EnvironmentType; + /** + * The new ID for the environment + */ + id: string; + }; +}; + +/** + * This interface was referenced by `Environment`'s JSON-Schema + * via the `rename.targetSchema` link. + */ +export type EnvironmentRenameTargetSchema = { + data: Environment; +}; + /** * This interface was referenced by `Environment`'s JSON-Schema * via the `instances.targetSchema` link. diff --git a/packages/cma-client/src/generated/SimpleSchemaTypes.ts b/packages/cma-client/src/generated/SimpleSchemaTypes.ts index 95677401..70c36780 100644 --- a/packages/cma-client/src/generated/SimpleSchemaTypes.ts +++ b/packages/cma-client/src/generated/SimpleSchemaTypes.ts @@ -1338,7 +1338,6 @@ export type EditingSessionUpdateSchema = */ type?: 'editing_session_enter_item'; item: ItemData; - [k: string]: unknown; } | { /** @@ -1346,7 +1345,6 @@ export type EditingSessionUpdateSchema = */ type?: 'editing_session_take_over_item'; item: ItemData; - [k: string]: unknown; } | { /** @@ -1354,14 +1352,12 @@ export type EditingSessionUpdateSchema = */ type?: 'editing_session_lock_item'; item: ItemData; - [k: string]: unknown; } | { /** * JSON API type */ type?: 'editing_session_unlock_item'; - [k: string]: unknown; }; /** * This interface was referenced by `EditingSession`'s JSON-Schema @@ -7359,7 +7355,6 @@ export type ItemBulkPublishSchema = { type?: 'item_bulk_publish_operation'; items: ItemData[]; minItems?: unknown; - [k: string]: unknown; }; /** @@ -7370,7 +7365,6 @@ export type ItemBulkUnpublishSchema = { type?: 'item_bulk_unpublish_operation'; items: ItemData[]; minItems?: unknown; - [k: string]: unknown; }; /** @@ -7381,7 +7375,6 @@ export type ItemBulkDestroySchema = { type?: 'item_bulk_destroy_operation'; items: ItemData[]; minItems?: unknown; - [k: string]: unknown; }; /** @@ -8024,7 +8017,6 @@ export type UploadBulkTagSchema = { export type UploadBulkDestroySchema = { type?: 'upload_bulk_destroy_operation'; uploads: UploadData[]; - [k: string]: unknown; }; /** @@ -8334,6 +8326,7 @@ export type Environment = { }; export type EnvironmentForkJobSchema = Environment; export type EnvironmentPromoteTargetSchema = Environment; +export type EnvironmentRenameTargetSchema = Environment; export type EnvironmentSelfTargetSchema = Environment; export type EnvironmentDestroyJobSchema = Environment; /** @@ -8392,9 +8385,20 @@ export type EnvironmentForkSchema = { /** * The ID of the forked environment */ - id?: string; + id: string; + type?: EnvironmentType; +}; + +/** + * This interface was referenced by `Environment`'s JSON-Schema + * via the `rename.schema` link. + */ +export type EnvironmentRenameSchema = { + /** + * The new ID for the environment + */ + id: string; type?: EnvironmentType; - [k: string]: unknown; }; /** @@ -9398,7 +9402,6 @@ export type SiteInvitationUpdateSchema = { id?: SiteInvitationIdentity; type?: SiteInvitationType; role?: RoleData; - [k: string]: unknown; }; /** diff --git a/packages/cma-client/src/generated/resources/Environment.ts b/packages/cma-client/src/generated/resources/Environment.ts index 9dab59ce..e2c02f39 100644 --- a/packages/cma-client/src/generated/resources/Environment.ts +++ b/packages/cma-client/src/generated/resources/Environment.ts @@ -89,6 +89,52 @@ export default class Environment extends BaseResource { }); } + /** + * Rename an environment + * + * Read more: https://www.datocms.com/docs/content-management-api/resources/environment/rename + * + * @throws {ApiError} + * @throws {TimeoutError} + */ + rename( + environmentId: string | SimpleSchemaTypes.EnvironmentData, + body: SimpleSchemaTypes.EnvironmentRenameSchema, + ) { + return this.rawRename( + Utils.toId(environmentId), + Utils.serializeRequestBody(body, { + id: Utils.toId(environmentId), + type: 'environment', + attributes: [], + relationships: [], + }), + ).then((body) => + Utils.deserializeResponseBody( + body, + ), + ); + } + + /** + * Rename an environment + * + * Read more: https://www.datocms.com/docs/content-management-api/resources/environment/rename + * + * @throws {ApiError} + * @throws {TimeoutError} + */ + rawRename( + environmentId: string, + body: SchemaTypes.EnvironmentRenameSchema, + ): Promise { + return this.client.request({ + method: 'PUT', + url: `/environments/${environmentId}/rename`, + body, + }); + } + /** * List all environments * diff --git a/packages/dashboard-client/src/generated/Client.ts b/packages/dashboard-client/src/generated/Client.ts index 8d50b002..df3d4027 100644 --- a/packages/dashboard-client/src/generated/Client.ts +++ b/packages/dashboard-client/src/generated/Client.ts @@ -110,7 +110,7 @@ export class Client { ...this.config, ...options, logFn: this.config.logFn || console.log, - userAgent: '@datocms/dashboard-client v3.1.8', + userAgent: '@datocms/dashboard-client', baseUrl: this.baseUrl, preCallStack: new Error().stack, extraHeaders: { diff --git a/packages/dashboard-client/src/generated/SimpleSchemaTypes.ts b/packages/dashboard-client/src/generated/SimpleSchemaTypes.ts index dd7c8aa6..21e3f5a1 100644 --- a/packages/dashboard-client/src/generated/SimpleSchemaTypes.ts +++ b/packages/dashboard-client/src/generated/SimpleSchemaTypes.ts @@ -2036,7 +2036,6 @@ export type PerOwnerPricingSubscriptionSimulateSchema = { export type PerOwnerPricingSubscriptionValidateSchema = { type?: PerOwnerPricingSubscriptionType; plan: PerOwnerPricingPlanData; - [k: string]: unknown; }; /** @@ -3823,7 +3822,6 @@ export type OrganizationInvitationUpdateSchema = { id?: OrganizationInvitationIdentity; type?: OrganizationInvitationType; role?: OrganizationRoleData; - [k: string]: unknown; }; /** @@ -3892,7 +3890,6 @@ export type OrganizationMembershipUpdateSchema = { id?: OrganizationMembershipIdentity; type?: OrganizationMembershipType; role?: OrganizationRoleData; - [k: string]: unknown; }; /** @@ -3958,7 +3955,6 @@ export type OrganizationMandateRequestRelationships = { export type OrganizationMandateRequestCreateSchema = { type?: OrganizationMandateRequestType; approver_organization: OrganizationData; - [k: string]: unknown; }; /** @@ -4027,5 +4023,4 @@ export type OrganizationMandateUpdateSchema = { id?: OrganizationMandateIdentity; type?: OrganizationMandateType; additional_enabled_plans?: PerOwnerPricingPlanData[]; - [k: string]: unknown; };