Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions scripts/generate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import path from 'path';
const GENERATED_DIR = path.join(process.cwd(), 'src', 'generated');
const OUTPUT_DIR = path.join(process.cwd(), 'src', 'types');

const DEEP_REQUIRED_TYPE = `
type DeepRequired<T> = T extends object
? { [K in keyof T]-?: DeepRequired<NonNullable<T[K]>> }
: T;
`;

function normalizeModuleName(file: string): string {
return file.replace('.d.ts', '').split(/[-_]/g).join('');
}

function toPascalCase(raw: string): string {
return raw
.replace(/-a-/g, '-') // drop -a-
.replace(/-an-/g, '-') // drop -an-
.replace(/-a-/g, '-')
.replace(/-an-/g, '-')
.split(/[-_]/g)
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
.join('');
Expand Down Expand Up @@ -59,7 +65,13 @@ function processModuleFile(alias: string, content: string): string[] {
if (seen.has(finalName)) continue;
seen.add(finalName);

out.push(`export type ${finalName} = ${alias}['schemas']['${raw}'];`);
if (finalName.endsWith('Response')) {
out.push(
`export type ${finalName} = DeepRequired<${alias}['schemas']['${raw}']>;`
);
} else {
out.push(`export type ${finalName} = ${alias}['schemas']['${raw}'];`);
}
}

return out;
Expand Down Expand Up @@ -96,7 +108,7 @@ function generate() {

const final = [
`import type { components as ${alias} } from '../generated/${modulePath}';`,
'',
DEEP_REQUIRED_TYPE,
...exports,
'',
].join('\n');
Expand Down
44 changes: 41 additions & 3 deletions specs/salesorders.yml
Original file line number Diff line number Diff line change
Expand Up @@ -903,10 +903,20 @@ components:
$ref: '#/components/schemas/total'
balance:
$ref: '#/components/schemas/balance'
attention:
type: string
description: "Name of the person to whose attention the shipment is sent."
example: "Marie-Louise Kurz"
company_name:
type: string
description: "Company name associated with the shipping address."
address:
description: "Name of the street of the customer's shipping address."
type: string
example: 'No:432,Bayside,Queens'
street2:
type: string
description: "Additional address line for the customer's shipping address."
city:
description: "Name of the city of the customer's shipping address."
type: string
Expand All @@ -915,6 +925,10 @@ components:
description: "Name of the state of the customer's shipping address."
type: string
example: 'New York'
state_code:
type: string
description: "Abbreviated code for the state."
example: "RP"
zip:
description: "Zip code of the customer's shipping address."
type: string
Expand All @@ -923,26 +937,42 @@ components:
description: "Name of the country of the customer's shipping address."
type: string
example: U.S.A
country_code:
type: string
description: "ISO code of the country."
example: "DE"
fax:
description: "Fax number of the customer's shipping address."
type: string
example: 424-524242
example: 424-524242
shipping_address:
description: "Customer's shipping address. It contains - <code>address</code>, <code>city</code>, <code>state</code>, <code>zip</code>, <code>country</code> and <code>fax</code>."
type: object
properties:
attention:
$ref: '#/components/schemas/attention'
company_name:
$ref: '#/components/schemas/company_name'
address:
$ref: '#/components/schemas/address'
street2:
$ref: '#/components/schemas/street2'
city:
$ref: '#/components/schemas/city'
state:
$ref: '#/components/schemas/state'
state_code:
$ref: '#/components/schemas/state_code'
zip:
$ref: '#/components/schemas/zip'
country:
$ref: '#/components/schemas/country'
country_code:
$ref: '#/components/schemas/country_code'
phone:
$ref: '#/components/schemas/phone'
fax:
$ref: '#/components/schemas/fax'
$ref: '#/components/schemas/fax'
shipping_address_id:
description: 'Unique Id generated by the server for address in contacts page. To add a shipping address to sales order, send the address_id using this node. Else, the default shipping address for that contact is used'
type: integer
Expand All @@ -954,16 +984,24 @@ components:
properties:
address:
$ref: '#/components/schemas/address'
street2:
$ref: '#/components/schemas/street2'
city:
$ref: '#/components/schemas/city'
state:
$ref: '#/components/schemas/state'
state_code:
$ref: '#/components/schemas/state_code'
zip:
$ref: '#/components/schemas/zip'
country:
$ref: '#/components/schemas/country'
country_code:
$ref: '#/components/schemas/country_code'
phone:
$ref: '#/components/schemas/phone'
fax:
$ref: '#/components/schemas/fax'
$ref: '#/components/schemas/fax'
billing_address_id:
description: 'Unique Id generated by the server for address in contacts page. To add a billing address to sales order, send the address_id using this node. Else, the default billing address for that contact is used'
type: integer
Expand Down