Skip to content

Commit

Permalink
chore: fix getDirName.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Aug 4, 2023
1 parent d729e4d commit ef1d89d
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 40 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"typescript.referencesCodeLens.enabled": true,
"typescript.tsdk": "node_modules/typescript/lib"
}
29 changes: 17 additions & 12 deletions aspida.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,61 @@ module.exports = [
{
input: 'samples/swagger',
outputEachDir: true,
openapi: { inputFile: 'samples/swagger.yaml' }
openapi: { inputFile: 'samples/swagger.yaml' },
},
{
input: 'samples/simple',
outputEachDir: true,
openapi: { inputFile: 'samples/simple.yaml' }
openapi: { inputFile: 'samples/simple.yaml' },
},
{
input: 'samples/strapi',
outputEachDir: true,
openapi: { inputFile: 'samples/strapi.json' }
openapi: { inputFile: 'samples/strapi.json' },
},
{
input: 'samples/freee',
outputEachDir: true,
openapi: { inputFile: 'samples/freee.json' }
openapi: { inputFile: 'samples/freee.json' },
},
{
input: 'samples/openapi',
outputEachDir: true,
openapi: { inputFile: 'samples/openapi.json', yaml: false }
openapi: { inputFile: 'samples/openapi.json', yaml: false },
},
{
input: 'samples/externals',
outputEachDir: true,
openapi: { inputFile: 'samples/externals.json', yaml: false }
openapi: { inputFile: 'samples/externals.json', yaml: false },
},
{
input: 'samples/nullable-object',
outputEachDir: true,
openapi: { inputFile: 'samples/nullable-object.yml' }
openapi: { inputFile: 'samples/nullable-object.yml' },
},
{
input: 'samples/array-one-of',
outputEachDir: true,
openapi: { inputFile: 'samples/array-one-of.yml' }
openapi: { inputFile: 'samples/array-one-of.yml' },
},
{
input: 'samples/request-bodies',
outputEachDir: true,
openapi: { inputFile: 'samples/request-bodies.yml' }
openapi: { inputFile: 'samples/request-bodies.yml' },
},
{
input: 'samples/responses',
outputEachDir: true,
openapi: { inputFile: 'samples/responses.yml' }
}
openapi: { inputFile: 'samples/responses.yml' },
},
{
input: 'samples/path',
outputEachDir: true,
openapi: { inputFile: 'samples/path.yml' },
},
// {
// input: 'samples/path-at-mark',
// outputEachDir: true,
// openapi: { inputFile: 'samples/path-at-mark.yml' }
// }
]
];
4 changes: 2 additions & 2 deletions samples/freee/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import type { Methods as Methods53 } from './api/1/users/me';
import type { Methods as Methods54 } from './api/1/wallet_txns';
import type { Methods as Methods55 } from './api/1/wallet_txns/_id@number';
import type { Methods as Methods56 } from './api/1/walletables';
import type { Methods as Methods57 } from './api/1/walletables/_type/_id@number';
import type { Methods as Methods57 } from './api/1/walletables/_type@string/_id@number';

const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const prefix = (baseURL === undefined ? 'https://api.freee.co.jp' : baseURL).replace(/\/$/, '');
Expand Down Expand Up @@ -4919,7 +4919,7 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
`${prefix}${PATH37}${option && option.query ? `?${dataToURLString(option.query)}` : ''}`,
},
walletables: {
_type: (val3: number | string) => {
_type: (val3: string) => {
const prefix3 = `${PATH38}/${val3}`;

return {
Expand Down
4 changes: 2 additions & 2 deletions samples/freee/api/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import type { Methods as Methods53 } from './1/users/me';
import type { Methods as Methods54 } from './1/wallet_txns';
import type { Methods as Methods55 } from './1/wallet_txns/_id@number';
import type { Methods as Methods56 } from './1/walletables';
import type { Methods as Methods57 } from './1/walletables/_type/_id@number';
import type { Methods as Methods57 } from './1/walletables/_type@string/_id@number';

const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const prefix = (baseURL === undefined ? 'https://api.freee.co.jp' : baseURL).replace(/\/$/, '');
Expand Down Expand Up @@ -4918,7 +4918,7 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
`${prefix}${PATH37}${option && option.query ? `?${dataToURLString(option.query)}` : ''}`,
},
walletables: {
_type: (val2: number | string) => {
_type: (val2: string) => {
const prefix2 = `${PATH38}/${val2}`;

return {
Expand Down
4 changes: 2 additions & 2 deletions samples/freee/api/1/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import type { Methods as Methods53 } from './users/me';
import type { Methods as Methods54 } from './wallet_txns';
import type { Methods as Methods55 } from './wallet_txns/_id@number';
import type { Methods as Methods56 } from './walletables';
import type { Methods as Methods57 } from './walletables/_type/_id@number';
import type { Methods as Methods57 } from './walletables/_type@string/_id@number';

const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const prefix = (baseURL === undefined ? 'https://api.freee.co.jp' : baseURL).replace(/\/$/, '');
Expand Down Expand Up @@ -4917,7 +4917,7 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
`${prefix}${PATH37}${option && option.query ? `?${dataToURLString(option.query)}` : ''}`,
},
walletables: {
_type: (val1: number | string) => {
_type: (val1: string) => {
const prefix1 = `${PATH38}/${val1}`;

return {
Expand Down
4 changes: 2 additions & 2 deletions samples/freee/api/1/walletables/$api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AspidaClient, BasicHeaders } from 'aspida';
import { dataToURLString } from 'aspida';
import type { Methods as Methods0 } from '.';
import type { Methods as Methods1 } from './_type/_id@number';
import type { Methods as Methods1 } from './_type@string/_id@number';

const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const prefix = (baseURL === undefined ? 'https://api.freee.co.jp' : baseURL).replace(/\/$/, '');
Expand All @@ -12,7 +12,7 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const DELETE = 'DELETE';

return {
_type: (val0: number | string) => {
_type: (val0: string) => {
const prefix0 = `${PATH0}/${val0}`;

return {
Expand Down
18 changes: 18 additions & 0 deletions samples/path.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
openapi: 3.0.0
info:
version: 1.0.0
title: Sample
paths:
"/invoices/{invoiceCategory}":
get:
parameters:
- name: invoiceCategory
in: path
required: true
schema:
"$ref": "#/components/schemas/InvoiceCategory"
components:
schemas:
InvoiceCategory:
type: string
enum: ["category-1", "category-2"]
23 changes: 23 additions & 0 deletions samples/path/$api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { AspidaClient } from 'aspida';


const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '');
const PATH0 = '/invoices';


return {
invoices: {
_invoiceCategory: (val1: string) => {
const prefix1 = `${PATH0}/${val1}`;

return {

};
},
},
};
};

export type ApiInstance = ReturnType<typeof api>;
export default api;
2 changes: 2 additions & 0 deletions samples/path/@types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable */
export type InvoiceCategory = 'category-1' | 'category-2'
21 changes: 21 additions & 0 deletions samples/path/invoices/$api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { AspidaClient } from 'aspida';


const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '');
const PATH0 = '/invoices';


return {
_invoiceCategory: (val0: string) => {
const prefix0 = `${PATH0}/${val0}`;

return {

};
},
};
};

export type ApiInstance = ReturnType<typeof api>;
export default api;
5 changes: 5 additions & 0 deletions samples/path/invoices/_invoiceCategory@string/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-disable */
export type Methods = {
get: {
}
}
3 changes: 1 addition & 2 deletions src/buildV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ export default (openapi: OpenAPIV3.Document) => {
[] as OpenAPIV3.ParameterObject[]
),
],
openapi,
false
openapi
)
),
'index',
Expand Down
40 changes: 22 additions & 18 deletions src/builderUtils/getDirName.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import type { OpenAPIV3 } from 'openapi-types';
import { getPropertyName, isRefObject, schema2value } from './converters';
import { getPropertyName, isRefObject } from './converters';
import { resolveSchemasRef } from './resolvers';

export default (
text: string,
params: OpenAPIV3.ParameterObject[],
openapi: OpenAPIV3.Document,
required: boolean
) => {
export default (text: string, params: OpenAPIV3.ParameterObject[], openapi: OpenAPIV3.Document) => {
if (text === '*') return '_any';
if (!/^{/.test(text)) {
return text;
}
if (!/^{/.test(text)) return text;

const valName = text.slice(1, -1);
const prefix = `_${getPropertyName(valName)}`;

const schema = params.find(p => p.in === 'path' && p.name === valName)?.schema;

if (!schema) return prefix;

let schema = params.find(p => p.in === 'path' && p.name === valName)?.schema;
if (schema && isRefObject(schema)) {
if (isRefObject(schema)) {
const referencedSchema = resolveSchemasRef(openapi, schema.$ref);
if (referencedSchema.type === 'string' || referencedSchema.type === 'number') {
schema = referencedSchema;

if (referencedSchema.type === 'string') {
return `${prefix}@string`;
} else if (referencedSchema.type === 'number' || referencedSchema.type === 'integer') {
return `${prefix}@number`;
} else {
return prefix;
}
}

const schemaVal = schema2value(schema, required);

return `_${getPropertyName(valName)}${
schemaVal && typeof schemaVal.value === 'string' ? `@${schemaVal.value}` : ''
return `${prefix}${
schema.type === 'string'
? '@string'
: schema.type === 'number' || schema.type === 'integer'
? '@number'
: ''
}`;
};

0 comments on commit ef1d89d

Please sign in to comment.