@rvoh/psychic 3.0.5 → 3.1.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.
@@ -32,4 +32,14 @@ export default class PackageManager {
32
32
  return `${this.packageManager} ${cmd}`;
33
33
  }
34
34
  }
35
+ static exec(cmd) {
36
+ switch (this.packageManager) {
37
+ case 'npm':
38
+ return `npm exec -- ${cmd}`;
39
+ case 'yarn':
40
+ return `yarn ${cmd}`;
41
+ default:
42
+ return `${this.packageManager} exec ${cmd}`;
43
+ }
44
+ }
35
45
  }
@@ -1,6 +1,7 @@
1
1
  import { camelize, pascalize } from '@rvoh/dream/utils';
2
2
  import * as fs from 'node:fs/promises';
3
3
  import * as path from 'node:path';
4
+ import PackageManager from '../../../cli/helpers/PackageManager.js';
4
5
  import psychicPath from '../../../helpers/path/psychicPath.js';
5
6
  export default async function writeInitializer({ exportName }) {
6
7
  const pascalized = pascalize(exportName);
@@ -22,6 +23,7 @@ export default async function writeInitializer({ exportName }) {
22
23
  await fs.mkdir(destDir, { recursive: true });
23
24
  }
24
25
  const filePath = path.join('.', 'src', 'conf', 'openapi', `${camelized}.openapi-codegen.json`);
26
+ const execCmd = PackageManager.exec(`@rtk-query/codegen-openapi ${filePath}`);
25
27
  const contents = `\
26
28
  import { DreamCLI } from '@rvoh/dream/system'
27
29
  import { PsychicApp } from '@rvoh/psychic'
@@ -31,7 +33,7 @@ export default function initialize${pascalized}(psy: PsychicApp) {
31
33
  psy.on('cli:sync', async () => {
32
34
  if (AppEnv.isDevelopmentOrTest) {
33
35
  await DreamCLI.logger.logProgress(\`[${camelized}] syncing...\`, async () => {
34
- await DreamCLI.spawn('npx @rtk-query/codegen-openapi ${filePath}', {
36
+ await DreamCLI.spawn('${execCmd}', {
35
37
  onStdout: message => {
36
38
  DreamCLI.logger.logContinueProgress(\`[${camelized}]\` + ' ' + message, {
37
39
  logPrefixColor: 'green',
@@ -1,6 +1,7 @@
1
1
  import { hyphenize } from '@rvoh/dream/utils';
2
2
  import * as fs from 'node:fs/promises';
3
3
  import * as path from 'node:path';
4
+ import PackageManager from '../../../cli/helpers/PackageManager.js';
4
5
  import psychicPath from '../../../helpers/path/psychicPath.js';
5
6
  export default async function generateInitializer(openapiFilepath, outfile, initializerFilename) {
6
7
  if (!/\.d\.ts$/.test(outfile))
@@ -15,6 +16,7 @@ export default async function generateInitializer(openapiFilepath, outfile, init
15
16
  catch {
16
17
  await fs.mkdir(destDir, { recursive: true });
17
18
  }
19
+ const execCmd = PackageManager.exec(`openapi-typescript ${openapiFilepath} -o ${outfile}`);
18
20
  const contents = `\
19
21
  import { DreamCLI } from '@rvoh/dream/system'
20
22
  import { PsychicApp } from "@rvoh/psychic"
@@ -24,7 +26,7 @@ export default (psy: PsychicApp) => {
24
26
  psy.on('cli:sync', async () => {
25
27
  if (AppEnv.isDevelopmentOrTest) {
26
28
  await DreamCLI.logger.logProgress(\`[${hyphenized}] extracting types from ${openapiFilepath} to ${outfile}...\`, async () => {
27
- await DreamCLI.spawn('npx openapi-typescript ${openapiFilepath} -o ${outfile}')
29
+ await DreamCLI.spawn('${execCmd}')
28
30
  })
29
31
  }
30
32
  })
@@ -1,10 +1,12 @@
1
1
  import { camelize, pascalize } from '@rvoh/dream/utils';
2
2
  import * as fs from 'node:fs/promises';
3
3
  import * as path from 'node:path';
4
+ import PackageManager from '../../../cli/helpers/PackageManager.js';
4
5
  import psychicPath from '../../../helpers/path/psychicPath.js';
5
6
  export default async function writeInitializer({ exportName, schemaFile, outputDir, }) {
6
7
  const pascalized = pascalize(exportName);
7
8
  const camelized = camelize(exportName);
9
+ const execCmd = PackageManager.exec(`@hey-api/openapi-ts -i ${schemaFile} -o ${outputDir}`);
8
10
  const destDir = path.join(psychicPath('conf'), 'initializers', 'openapi');
9
11
  const initializerFilename = `${camelized}.ts`;
10
12
  const initializerPath = path.join(destDir, initializerFilename);
@@ -30,7 +32,7 @@ export default function initialize${pascalized}(psy: PsychicApp) {
30
32
  psy.on('cli:sync', async () => {
31
33
  if (AppEnv.isDevelopmentOrTest) {
32
34
  await DreamCLI.logger.logProgress(\`[${camelized}] syncing...\`, async () => {
33
- await DreamCLI.spawn('npx @hey-api/openapi-ts -i ${schemaFile} -o ${outputDir}', {
35
+ await DreamCLI.spawn('${execCmd}', {
34
36
  onStdout: message => {
35
37
  DreamCLI.logger.logContinueProgress(\`[${camelized}]\` + ' ' + message, {
36
38
  logPrefixColor: 'green',
@@ -139,13 +139,16 @@ export default class SerializerOpenapiRenderer {
139
139
  const openapi = attribute.options.openapi;
140
140
  newlyReferencedSerializers = allSerializersFromHandWrittenOpenapi(openapi);
141
141
  let target;
142
+ let delegatedAssociationOptional = false;
142
143
  if (attributeType === 'delegatedAttribute' && DataTypeForOpenapi?.isDream) {
143
144
  const source = DataTypeForOpenapi;
144
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
145
- const associatedModelOrModels = source['getAssociationMetadata'](attribute.targetName)?.modelCB();
145
+ const association = source['getAssociationMetadata'](attribute.targetName);
146
+ const associatedModelOrModels = association?.modelCB();
146
147
  target = Array.isArray(associatedModelOrModels)
147
148
  ? associatedModelOrModels[0]
148
149
  : associatedModelOrModels;
150
+ delegatedAssociationOptional = !!association
151
+ ?.optional;
149
152
  }
150
153
  else if (attributeType === 'delegatedAttribute') {
151
154
  target = undefined;
@@ -153,11 +156,36 @@ export default class SerializerOpenapiRenderer {
153
156
  else {
154
157
  target = DataTypeForOpenapi;
155
158
  }
156
- accumulator[outputAttributeName] = allSerializersToRefsInOpenapi(target?.isDream
159
+ const resolvedSchema = allSerializersToRefsInOpenapi(target?.isDream
157
160
  ? dreamColumnOpenapiShape(this.serializer.globalName, target, attribute.name, openapi, {
158
161
  suppressResponseEnums: this.suppressResponseEnums,
159
162
  })
160
163
  : openapiShorthandToOpenapi(openapi));
164
+ const optional = attributeType === 'delegatedAttribute' &&
165
+ (attribute.options.optional ?? delegatedAssociationOptional);
166
+ if (optional && !openapiSchemaIncludesNull(resolvedSchema)) {
167
+ const schemaRecord = resolvedSchema;
168
+ if (typeof schemaRecord.type === 'string') {
169
+ accumulator[outputAttributeName] = {
170
+ ...schemaRecord,
171
+ type: [schemaRecord.type, 'null'],
172
+ };
173
+ }
174
+ else if (Array.isArray(schemaRecord.type)) {
175
+ accumulator[outputAttributeName] = {
176
+ ...schemaRecord,
177
+ type: [...schemaRecord.type, 'null'],
178
+ };
179
+ }
180
+ else {
181
+ accumulator[outputAttributeName] = {
182
+ anyOf: [resolvedSchema, NULL_OBJECT_OPENAPI],
183
+ };
184
+ }
185
+ }
186
+ else {
187
+ accumulator[outputAttributeName] = resolvedSchema;
188
+ }
161
189
  return accumulator;
162
190
  }
163
191
  /////////////////////////////////////////////
@@ -375,3 +403,16 @@ function descendantSerializers(serializer, alreadyExtractedDescendantSerializers
375
403
  // throws an error)
376
404
  class CallingSerializersThrewError extends Error {
377
405
  }
406
+ function openapiSchemaIncludesNull(schema) {
407
+ if (typeof schema !== 'object' || schema === null)
408
+ return false;
409
+ const schemaRecord = schema;
410
+ if (Array.isArray(schemaRecord.type) && schemaRecord.type.includes('null'))
411
+ return true;
412
+ if (schemaRecord.type === 'null')
413
+ return true;
414
+ if (Array.isArray(schemaRecord.anyOf) &&
415
+ schemaRecord.anyOf.some((member) => openapiSchemaIncludesNull(member)))
416
+ return true;
417
+ return false;
418
+ }
@@ -32,4 +32,14 @@ export default class PackageManager {
32
32
  return `${this.packageManager} ${cmd}`;
33
33
  }
34
34
  }
35
+ static exec(cmd) {
36
+ switch (this.packageManager) {
37
+ case 'npm':
38
+ return `npm exec -- ${cmd}`;
39
+ case 'yarn':
40
+ return `yarn ${cmd}`;
41
+ default:
42
+ return `${this.packageManager} exec ${cmd}`;
43
+ }
44
+ }
35
45
  }
@@ -1,6 +1,7 @@
1
1
  import { camelize, pascalize } from '@rvoh/dream/utils';
2
2
  import * as fs from 'node:fs/promises';
3
3
  import * as path from 'node:path';
4
+ import PackageManager from '../../../cli/helpers/PackageManager.js';
4
5
  import psychicPath from '../../../helpers/path/psychicPath.js';
5
6
  export default async function writeInitializer({ exportName }) {
6
7
  const pascalized = pascalize(exportName);
@@ -22,6 +23,7 @@ export default async function writeInitializer({ exportName }) {
22
23
  await fs.mkdir(destDir, { recursive: true });
23
24
  }
24
25
  const filePath = path.join('.', 'src', 'conf', 'openapi', `${camelized}.openapi-codegen.json`);
26
+ const execCmd = PackageManager.exec(`@rtk-query/codegen-openapi ${filePath}`);
25
27
  const contents = `\
26
28
  import { DreamCLI } from '@rvoh/dream/system'
27
29
  import { PsychicApp } from '@rvoh/psychic'
@@ -31,7 +33,7 @@ export default function initialize${pascalized}(psy: PsychicApp) {
31
33
  psy.on('cli:sync', async () => {
32
34
  if (AppEnv.isDevelopmentOrTest) {
33
35
  await DreamCLI.logger.logProgress(\`[${camelized}] syncing...\`, async () => {
34
- await DreamCLI.spawn('npx @rtk-query/codegen-openapi ${filePath}', {
36
+ await DreamCLI.spawn('${execCmd}', {
35
37
  onStdout: message => {
36
38
  DreamCLI.logger.logContinueProgress(\`[${camelized}]\` + ' ' + message, {
37
39
  logPrefixColor: 'green',
@@ -1,6 +1,7 @@
1
1
  import { hyphenize } from '@rvoh/dream/utils';
2
2
  import * as fs from 'node:fs/promises';
3
3
  import * as path from 'node:path';
4
+ import PackageManager from '../../../cli/helpers/PackageManager.js';
4
5
  import psychicPath from '../../../helpers/path/psychicPath.js';
5
6
  export default async function generateInitializer(openapiFilepath, outfile, initializerFilename) {
6
7
  if (!/\.d\.ts$/.test(outfile))
@@ -15,6 +16,7 @@ export default async function generateInitializer(openapiFilepath, outfile, init
15
16
  catch {
16
17
  await fs.mkdir(destDir, { recursive: true });
17
18
  }
19
+ const execCmd = PackageManager.exec(`openapi-typescript ${openapiFilepath} -o ${outfile}`);
18
20
  const contents = `\
19
21
  import { DreamCLI } from '@rvoh/dream/system'
20
22
  import { PsychicApp } from "@rvoh/psychic"
@@ -24,7 +26,7 @@ export default (psy: PsychicApp) => {
24
26
  psy.on('cli:sync', async () => {
25
27
  if (AppEnv.isDevelopmentOrTest) {
26
28
  await DreamCLI.logger.logProgress(\`[${hyphenized}] extracting types from ${openapiFilepath} to ${outfile}...\`, async () => {
27
- await DreamCLI.spawn('npx openapi-typescript ${openapiFilepath} -o ${outfile}')
29
+ await DreamCLI.spawn('${execCmd}')
28
30
  })
29
31
  }
30
32
  })
@@ -1,10 +1,12 @@
1
1
  import { camelize, pascalize } from '@rvoh/dream/utils';
2
2
  import * as fs from 'node:fs/promises';
3
3
  import * as path from 'node:path';
4
+ import PackageManager from '../../../cli/helpers/PackageManager.js';
4
5
  import psychicPath from '../../../helpers/path/psychicPath.js';
5
6
  export default async function writeInitializer({ exportName, schemaFile, outputDir, }) {
6
7
  const pascalized = pascalize(exportName);
7
8
  const camelized = camelize(exportName);
9
+ const execCmd = PackageManager.exec(`@hey-api/openapi-ts -i ${schemaFile} -o ${outputDir}`);
8
10
  const destDir = path.join(psychicPath('conf'), 'initializers', 'openapi');
9
11
  const initializerFilename = `${camelized}.ts`;
10
12
  const initializerPath = path.join(destDir, initializerFilename);
@@ -30,7 +32,7 @@ export default function initialize${pascalized}(psy: PsychicApp) {
30
32
  psy.on('cli:sync', async () => {
31
33
  if (AppEnv.isDevelopmentOrTest) {
32
34
  await DreamCLI.logger.logProgress(\`[${camelized}] syncing...\`, async () => {
33
- await DreamCLI.spawn('npx @hey-api/openapi-ts -i ${schemaFile} -o ${outputDir}', {
35
+ await DreamCLI.spawn('${execCmd}', {
34
36
  onStdout: message => {
35
37
  DreamCLI.logger.logContinueProgress(\`[${camelized}]\` + ' ' + message, {
36
38
  logPrefixColor: 'green',
@@ -139,13 +139,16 @@ export default class SerializerOpenapiRenderer {
139
139
  const openapi = attribute.options.openapi;
140
140
  newlyReferencedSerializers = allSerializersFromHandWrittenOpenapi(openapi);
141
141
  let target;
142
+ let delegatedAssociationOptional = false;
142
143
  if (attributeType === 'delegatedAttribute' && DataTypeForOpenapi?.isDream) {
143
144
  const source = DataTypeForOpenapi;
144
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
145
- const associatedModelOrModels = source['getAssociationMetadata'](attribute.targetName)?.modelCB();
145
+ const association = source['getAssociationMetadata'](attribute.targetName);
146
+ const associatedModelOrModels = association?.modelCB();
146
147
  target = Array.isArray(associatedModelOrModels)
147
148
  ? associatedModelOrModels[0]
148
149
  : associatedModelOrModels;
150
+ delegatedAssociationOptional = !!association
151
+ ?.optional;
149
152
  }
150
153
  else if (attributeType === 'delegatedAttribute') {
151
154
  target = undefined;
@@ -153,11 +156,36 @@ export default class SerializerOpenapiRenderer {
153
156
  else {
154
157
  target = DataTypeForOpenapi;
155
158
  }
156
- accumulator[outputAttributeName] = allSerializersToRefsInOpenapi(target?.isDream
159
+ const resolvedSchema = allSerializersToRefsInOpenapi(target?.isDream
157
160
  ? dreamColumnOpenapiShape(this.serializer.globalName, target, attribute.name, openapi, {
158
161
  suppressResponseEnums: this.suppressResponseEnums,
159
162
  })
160
163
  : openapiShorthandToOpenapi(openapi));
164
+ const optional = attributeType === 'delegatedAttribute' &&
165
+ (attribute.options.optional ?? delegatedAssociationOptional);
166
+ if (optional && !openapiSchemaIncludesNull(resolvedSchema)) {
167
+ const schemaRecord = resolvedSchema;
168
+ if (typeof schemaRecord.type === 'string') {
169
+ accumulator[outputAttributeName] = {
170
+ ...schemaRecord,
171
+ type: [schemaRecord.type, 'null'],
172
+ };
173
+ }
174
+ else if (Array.isArray(schemaRecord.type)) {
175
+ accumulator[outputAttributeName] = {
176
+ ...schemaRecord,
177
+ type: [...schemaRecord.type, 'null'],
178
+ };
179
+ }
180
+ else {
181
+ accumulator[outputAttributeName] = {
182
+ anyOf: [resolvedSchema, NULL_OBJECT_OPENAPI],
183
+ };
184
+ }
185
+ }
186
+ else {
187
+ accumulator[outputAttributeName] = resolvedSchema;
188
+ }
161
189
  return accumulator;
162
190
  }
163
191
  /////////////////////////////////////////////
@@ -375,3 +403,16 @@ function descendantSerializers(serializer, alreadyExtractedDescendantSerializers
375
403
  // throws an error)
376
404
  class CallingSerializersThrewError extends Error {
377
405
  }
406
+ function openapiSchemaIncludesNull(schema) {
407
+ if (typeof schema !== 'object' || schema === null)
408
+ return false;
409
+ const schemaRecord = schema;
410
+ if (Array.isArray(schemaRecord.type) && schemaRecord.type.includes('null'))
411
+ return true;
412
+ if (schemaRecord.type === 'null')
413
+ return true;
414
+ if (Array.isArray(schemaRecord.anyOf) &&
415
+ schemaRecord.anyOf.some((member) => openapiSchemaIncludesNull(member)))
416
+ return true;
417
+ return false;
418
+ }
@@ -4,4 +4,5 @@ export default class PackageManager {
4
4
  dev?: boolean;
5
5
  }): string;
6
6
  static run(cmd: string): string;
7
+ static exec(cmd: string): string;
7
8
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "type": "module",
3
3
  "name": "@rvoh/psychic",
4
4
  "description": "Typescript web framework",
5
- "version": "3.0.5",
5
+ "version": "3.1.0",
6
6
  "author": "RVOHealth",
7
7
  "repository": {
8
8
  "type": "git",
@@ -75,7 +75,7 @@
75
75
  "@koa/cors": "^5.0.0",
76
76
  "@koa/etag": "^5.0.2",
77
77
  "@koa/router": "^15.3.0",
78
- "@rvoh/dream": "^2.4.0",
78
+ "@rvoh/dream": "^2.6.0",
79
79
  "@types/koa": "^3.0.1",
80
80
  "@types/koa-bodyparser": "^4.3.12",
81
81
  "@types/koa-conditional-get": "^2.0.3",
@@ -92,7 +92,7 @@
92
92
  "@koa/cors": "^5.0.0",
93
93
  "@koa/etag": "^5.0.2",
94
94
  "@koa/router": "^15.3.1",
95
- "@rvoh/dream": "^2.4.0",
95
+ "@rvoh/dream": "^2.6.0",
96
96
  "@rvoh/dream-spec-helpers": "^2.1.1",
97
97
  "@rvoh/psychic-spec-helpers": "3.0.0",
98
98
  "@types/koa": "^3.0.1",