Skip to content

Commit

Permalink
feat: add outputEachDir option to config
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Jun 24, 2020
1 parent e27501c commit 6122a21
Show file tree
Hide file tree
Showing 14 changed files with 371 additions and 11 deletions.
7 changes: 6 additions & 1 deletion packages/aspida-mock/__tests__/lib/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ describe('cli', () => {
configs.forEach(config => {
const resultFilePath = `${config.resultDirPath}/$mock.ts`
const result = fs.readFileSync(resultFilePath, 'utf8')
const { text, filePath } = build({ input: config.input, baseURL: '', trailingSlash: false })
const { text, filePath } = build({
input: config.input,
baseURL: '',
trailingSlash: false,
outputEachDir: false
})

expect(text).toBe(result)
expect(filePath).toBe(resultFilePath)
Expand Down
11 changes: 9 additions & 2 deletions packages/aspida/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ const basePath = 'packages/aspida'

describe('cli test', () => {
test('main', () => {
const { input, baseURL, trailingSlash } = getConfig(`${basePath}/aspida.config.js`)[0]
const { input, baseURL, trailingSlash, outputEachDir } = getConfig(
`${basePath}/aspida.config.js`
)[0]
const inputDir = `${basePath}/${input}`
const inputs = [inputDir, `./${inputDir}`, `./${inputDir}/`, `${inputDir}/`]

inputs.forEach(inputPath => {
const resultFilePath = `${inputDir}/$api.ts`
const result = fs.readFileSync(resultFilePath, 'utf8')
const { filePath, text } = build({ input: inputPath, baseURL, trailingSlash })
const [{ filePath, text }] = build({
input: inputPath,
baseURL,
trailingSlash,
outputEachDir
})

expect(filePath).toBe(resultFilePath)
expect(text).toBe(result)
Expand Down
Empty file.
Empty file.
124 changes: 124 additions & 0 deletions packages/aspida/sample2/v1.1/$api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* eslint-disable */
import { AspidaClient, BasicHeaders } from 'aspida'
import * as ApiTypes from '../@types'
import { Methods as Methods0 } from './index'
import { Methods as Methods1 } from './2/_hogeId@HogeId/entries.json'
import { Methods as Methods2 } from './2/_hogeId@HogeId/test-4'
import { Methods as Methods3 } from './2/_hogeId@HogeId/test-4/_fugaId'
import { Methods as Methods4 } from './2/_hogeId@HogeId/test-4/fuga aa'
import { Methods as Methods5 } from './2/_hogeId@number'
import { Methods as Methods6 } from './3.1'
import { Methods as Methods7 } from './_articleId.json'
import { Methods as Methods8 } from './users/_userId@User[\'id\']'

const api = <T>(client: AspidaClient<T>) => {
const prefix = `${(client.baseURL === undefined ? '' : client.baseURL).replace(/\/$/, '')}/v1.1`

return {
$2: {
_hogeId_0: (val0: ApiTypes.HogeId) => ({
entries_json: {
get: (option?: { config?: T }) =>
client.fetch<Methods1['get']['resBody']>(prefix, `/2/${val0}/entries.json/`, 'GET', option).json(),
$get: async (option?: { config?: T }) =>
(await client.fetch<Methods1['get']['resBody']>(prefix, `/2/${val0}/entries.json/`, 'GET', option).json()).body
},
test_4: {
_fugaId: (val1: number | string) => ({
get: (option?: { query?: Methods3['get']['query'], config?: T }) =>
client.fetch<Methods3['get']['resBody']>(prefix, `/2/${val0}/test-4/${val1}/`, 'GET', option).json(),
$get: async (option?: { query?: Methods3['get']['query'], config?: T }) =>
(await client.fetch<Methods3['get']['resBody']>(prefix, `/2/${val0}/test-4/${val1}/`, 'GET', option).json()).body,
post: (option: { body?: Methods3['post']['reqBody'], query: Methods3['post']['query'], config?: T }) =>
client.fetch<Methods3['post']['resBody']>(prefix, `/2/${val0}/test-4/${val1}/`, 'POST', option).json(),
$post: async (option: { body?: Methods3['post']['reqBody'], query: Methods3['post']['query'], config?: T }) =>
(await client.fetch<Methods3['post']['resBody']>(prefix, `/2/${val0}/test-4/${val1}/`, 'POST', option).json()).body,
put: (option: { query: Methods3['put']['query'], config?: T }) =>
client.fetch<Methods3['put']['resBody']>(prefix, `/2/${val0}/test-4/${val1}/`, 'PUT', option).json(),
$put: async (option: { query: Methods3['put']['query'], config?: T }) =>
(await client.fetch<Methods3['put']['resBody']>(prefix, `/2/${val0}/test-4/${val1}/`, 'PUT', option).json()).body,
delete: (option: { query: Methods3['delete']['query'], config?: T }) =>
client.fetch<Methods3['delete']['resBody']>(prefix, `/2/${val0}/test-4/${val1}/`, 'DELETE', option).json(),
$delete: async (option: { query: Methods3['delete']['query'], config?: T }) =>
(await client.fetch<Methods3['delete']['resBody']>(prefix, `/2/${val0}/test-4/${val1}/`, 'DELETE', option).json()).body
}),
fuga_aa: {
get: (option: { query: Methods4['get']['query'], config?: T }) =>
client.fetch<Methods4['get']['resBody']>(prefix, `/2/${val0}/test-4/fuga aa/`, 'GET', option).json(),
$get: async (option: { query: Methods4['get']['query'], config?: T }) =>
(await client.fetch<Methods4['get']['resBody']>(prefix, `/2/${val0}/test-4/fuga aa/`, 'GET', option).json()).body,
post: (option: { body?: Methods4['post']['reqBody'], query: Methods4['post']['query'], config?: T }) =>
client.fetch<Methods4['post']['resBody']>(prefix, `/2/${val0}/test-4/fuga aa/`, 'POST', option).json(),
$post: async (option: { body?: Methods4['post']['reqBody'], query: Methods4['post']['query'], config?: T }) =>
(await client.fetch<Methods4['post']['resBody']>(prefix, `/2/${val0}/test-4/fuga aa/`, 'POST', option).json()).body,
put: (option: { query: Methods4['put']['query'], config?: T }) =>
client.fetch<Methods4['put']['resBody']>(prefix, `/2/${val0}/test-4/fuga aa/`, 'PUT', option).json(),
$put: async (option: { query: Methods4['put']['query'], config?: T }) =>
(await client.fetch<Methods4['put']['resBody']>(prefix, `/2/${val0}/test-4/fuga aa/`, 'PUT', option).json()).body,
delete: (option: { body: Methods4['delete']['reqBody'], query: Methods4['delete']['query'], config?: T }) =>
client.fetch<Methods4['delete']['resBody']>(prefix, `/2/${val0}/test-4/fuga aa/`, 'DELETE', option).json(),
$delete: async (option: { body: Methods4['delete']['reqBody'], query: Methods4['delete']['query'], config?: T }) =>
(await client.fetch<Methods4['delete']['resBody']>(prefix, `/2/${val0}/test-4/fuga aa/`, 'DELETE', option).json()).body
},
get: (option: { query: Methods2['get']['query'], config?: T }) =>
client.fetch<void>(prefix, `/2/${val0}/test-4/`, 'GET', option).send(),
$get: async (option: { query: Methods2['get']['query'], config?: T }) =>
(await client.fetch<void>(prefix, `/2/${val0}/test-4/`, 'GET', option).send()).body,
post: (option?: { body?: Methods2['post']['reqBody'], query?: Methods2['post']['query'], config?: T }) =>
client.fetch<void>(prefix, `/2/${val0}/test-4/`, 'POST', option).send(),
$post: async (option?: { body?: Methods2['post']['reqBody'], query?: Methods2['post']['query'], config?: T }) =>
(await client.fetch<void>(prefix, `/2/${val0}/test-4/`, 'POST', option).send()).body,
put: (option?: { query?: Methods2['put']['query'], config?: T }) =>
client.fetch<Methods2['put']['resBody']>(prefix, `/2/${val0}/test-4/`, 'PUT', option).json(),
$put: async (option?: { query?: Methods2['put']['query'], config?: T }) =>
(await client.fetch<Methods2['put']['resBody']>(prefix, `/2/${val0}/test-4/`, 'PUT', option).json()).body,
delete: (option: { query: Methods2['delete']['query'], config?: T }) =>
client.fetch<Methods2['delete']['resBody']>(prefix, `/2/${val0}/test-4/`, 'DELETE', option).json(),
$delete: async (option: { query: Methods2['delete']['query'], config?: T }) =>
(await client.fetch<Methods2['delete']['resBody']>(prefix, `/2/${val0}/test-4/`, 'DELETE', option).json()).body
}
}),
_hogeId_1: (val2: number) => ({
get: (option: { query?: Methods5['get']['query'], headers: Methods5['get']['reqHeaders'], config?: T }) =>
client.fetch<Methods5['get']['resBody']>(prefix, `/2/${val2}/`, 'GET', option).json(),
$get: async (option: { query?: Methods5['get']['query'], headers: Methods5['get']['reqHeaders'], config?: T }) =>
(await client.fetch<Methods5['get']['resBody']>(prefix, `/2/${val2}/`, 'GET', option).json()).body
})
},
$3_1: {
get: (option: { query?: Methods6['get']['query'], headers: Methods6['get']['reqHeaders'], config?: T }) =>
client.fetch<Methods6['get']['resBody']>(prefix, '/3.1/', 'GET', option).json(),
$get: async (option: { query?: Methods6['get']['query'], headers: Methods6['get']['reqHeaders'], config?: T }) =>
(await client.fetch<Methods6['get']['resBody']>(prefix, '/3.1/', 'GET', option).json()).body,
post: (option: { body?: Methods6['post']['reqBody'], query: Methods6['post']['query'], config?: T }) =>
client.fetch<Methods6['post']['resBody']>(prefix, '/3.1/', 'POST', option, 'URLSearchParams').json(),
$post: async (option: { body?: Methods6['post']['reqBody'], query: Methods6['post']['query'], config?: T }) =>
(await client.fetch<Methods6['post']['resBody']>(prefix, '/3.1/', 'POST', option, 'URLSearchParams').json()).body
},
_articleId_json: (val3: number | string) => ({
get: (option?: { config?: T }) =>
client.fetch<Methods7['get']['resBody']>(prefix, `/${val3}.json/`, 'GET', option).json(),
$get: async (option?: { config?: T }) =>
(await client.fetch<Methods7['get']['resBody']>(prefix, `/${val3}.json/`, 'GET', option).json()).body
}),
users: {
_userId: (val4: ApiTypes.User['id']) => ({
get: (option: { query: Methods8['get']['query'], headers: Methods8['get']['reqHeaders'], config?: T }) =>
client.fetch<Methods8['get']['resBody']>(prefix, `/users/${val4}/`, 'GET', option).json(),
$get: async (option: { query: Methods8['get']['query'], headers: Methods8['get']['reqHeaders'], config?: T }) =>
(await client.fetch<Methods8['get']['resBody']>(prefix, `/users/${val4}/`, 'GET', option).json()).body,
post: (option: { query: Methods8['post']['query'], config?: T }) =>
client.fetch<Methods8['post']['resBody']>(prefix, `/users/${val4}/`, 'POST', option).json(),
$post: async (option: { query: Methods8['post']['query'], config?: T }) =>
(await client.fetch<Methods8['post']['resBody']>(prefix, `/users/${val4}/`, 'POST', option).json()).body
})
},
get: (option?: { query?: Methods0['get']['query'], config?: T }) =>
client.fetch<Methods0['get']['resBody'], BasicHeaders, Methods0['get']['status']>(prefix, '/', 'GET', option).json(),
$get: async (option?: { query?: Methods0['get']['query'], config?: T }) =>
(await client.fetch<Methods0['get']['resBody'], BasicHeaders, Methods0['get']['status']>(prefix, '/', 'GET', option).json()).body
}
}

export type ApiInstance = ReturnType<typeof api>
export default api
86 changes: 86 additions & 0 deletions packages/aspida/sample2/v1.1/2/$api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* eslint-disable */
import { AspidaClient } from 'aspida'
import * as ApiTypes from '../../@types'
import { Methods as Methods0 } from './_hogeId@HogeId/entries.json'
import { Methods as Methods1 } from './_hogeId@HogeId/test-4'
import { Methods as Methods2 } from './_hogeId@HogeId/test-4/_fugaId'
import { Methods as Methods3 } from './_hogeId@HogeId/test-4/fuga aa'
import { Methods as Methods4 } from './_hogeId@number'

const api = <T>(client: AspidaClient<T>) => {
const prefix = `${(client.baseURL === undefined ? '' : client.baseURL).replace(/\/$/, '')}/v1.1/2`

return {
_hogeId_0: (val0: ApiTypes.HogeId) => ({
entries_json: {
get: (option?: { config?: T }) =>
client.fetch<Methods0['get']['resBody']>(prefix, `/${val0}/entries.json/`, 'GET', option).json(),
$get: async (option?: { config?: T }) =>
(await client.fetch<Methods0['get']['resBody']>(prefix, `/${val0}/entries.json/`, 'GET', option).json()).body
},
test_4: {
_fugaId: (val1: number | string) => ({
get: (option?: { query?: Methods2['get']['query'], config?: T }) =>
client.fetch<Methods2['get']['resBody']>(prefix, `/${val0}/test-4/${val1}/`, 'GET', option).json(),
$get: async (option?: { query?: Methods2['get']['query'], config?: T }) =>
(await client.fetch<Methods2['get']['resBody']>(prefix, `/${val0}/test-4/${val1}/`, 'GET', option).json()).body,
post: (option: { body?: Methods2['post']['reqBody'], query: Methods2['post']['query'], config?: T }) =>
client.fetch<Methods2['post']['resBody']>(prefix, `/${val0}/test-4/${val1}/`, 'POST', option).json(),
$post: async (option: { body?: Methods2['post']['reqBody'], query: Methods2['post']['query'], config?: T }) =>
(await client.fetch<Methods2['post']['resBody']>(prefix, `/${val0}/test-4/${val1}/`, 'POST', option).json()).body,
put: (option: { query: Methods2['put']['query'], config?: T }) =>
client.fetch<Methods2['put']['resBody']>(prefix, `/${val0}/test-4/${val1}/`, 'PUT', option).json(),
$put: async (option: { query: Methods2['put']['query'], config?: T }) =>
(await client.fetch<Methods2['put']['resBody']>(prefix, `/${val0}/test-4/${val1}/`, 'PUT', option).json()).body,
delete: (option: { query: Methods2['delete']['query'], config?: T }) =>
client.fetch<Methods2['delete']['resBody']>(prefix, `/${val0}/test-4/${val1}/`, 'DELETE', option).json(),
$delete: async (option: { query: Methods2['delete']['query'], config?: T }) =>
(await client.fetch<Methods2['delete']['resBody']>(prefix, `/${val0}/test-4/${val1}/`, 'DELETE', option).json()).body
}),
fuga_aa: {
get: (option: { query: Methods3['get']['query'], config?: T }) =>
client.fetch<Methods3['get']['resBody']>(prefix, `/${val0}/test-4/fuga aa/`, 'GET', option).json(),
$get: async (option: { query: Methods3['get']['query'], config?: T }) =>
(await client.fetch<Methods3['get']['resBody']>(prefix, `/${val0}/test-4/fuga aa/`, 'GET', option).json()).body,
post: (option: { body?: Methods3['post']['reqBody'], query: Methods3['post']['query'], config?: T }) =>
client.fetch<Methods3['post']['resBody']>(prefix, `/${val0}/test-4/fuga aa/`, 'POST', option).json(),
$post: async (option: { body?: Methods3['post']['reqBody'], query: Methods3['post']['query'], config?: T }) =>
(await client.fetch<Methods3['post']['resBody']>(prefix, `/${val0}/test-4/fuga aa/`, 'POST', option).json()).body,
put: (option: { query: Methods3['put']['query'], config?: T }) =>
client.fetch<Methods3['put']['resBody']>(prefix, `/${val0}/test-4/fuga aa/`, 'PUT', option).json(),
$put: async (option: { query: Methods3['put']['query'], config?: T }) =>
(await client.fetch<Methods3['put']['resBody']>(prefix, `/${val0}/test-4/fuga aa/`, 'PUT', option).json()).body,
delete: (option: { body: Methods3['delete']['reqBody'], query: Methods3['delete']['query'], config?: T }) =>
client.fetch<Methods3['delete']['resBody']>(prefix, `/${val0}/test-4/fuga aa/`, 'DELETE', option).json(),
$delete: async (option: { body: Methods3['delete']['reqBody'], query: Methods3['delete']['query'], config?: T }) =>
(await client.fetch<Methods3['delete']['resBody']>(prefix, `/${val0}/test-4/fuga aa/`, 'DELETE', option).json()).body
},
get: (option: { query: Methods1['get']['query'], config?: T }) =>
client.fetch<void>(prefix, `/${val0}/test-4/`, 'GET', option).send(),
$get: async (option: { query: Methods1['get']['query'], config?: T }) =>
(await client.fetch<void>(prefix, `/${val0}/test-4/`, 'GET', option).send()).body,
post: (option?: { body?: Methods1['post']['reqBody'], query?: Methods1['post']['query'], config?: T }) =>
client.fetch<void>(prefix, `/${val0}/test-4/`, 'POST', option).send(),
$post: async (option?: { body?: Methods1['post']['reqBody'], query?: Methods1['post']['query'], config?: T }) =>
(await client.fetch<void>(prefix, `/${val0}/test-4/`, 'POST', option).send()).body,
put: (option?: { query?: Methods1['put']['query'], config?: T }) =>
client.fetch<Methods1['put']['resBody']>(prefix, `/${val0}/test-4/`, 'PUT', option).json(),
$put: async (option?: { query?: Methods1['put']['query'], config?: T }) =>
(await client.fetch<Methods1['put']['resBody']>(prefix, `/${val0}/test-4/`, 'PUT', option).json()).body,
delete: (option: { query: Methods1['delete']['query'], config?: T }) =>
client.fetch<Methods1['delete']['resBody']>(prefix, `/${val0}/test-4/`, 'DELETE', option).json(),
$delete: async (option: { query: Methods1['delete']['query'], config?: T }) =>
(await client.fetch<Methods1['delete']['resBody']>(prefix, `/${val0}/test-4/`, 'DELETE', option).json()).body
}
}),
_hogeId_1: (val2: number) => ({
get: (option: { query?: Methods4['get']['query'], headers: Methods4['get']['reqHeaders'], config?: T }) =>
client.fetch<Methods4['get']['resBody']>(prefix, `/${val2}/`, 'GET', option).json(),
$get: async (option: { query?: Methods4['get']['query'], headers: Methods4['get']['reqHeaders'], config?: T }) =>
(await client.fetch<Methods4['get']['resBody']>(prefix, `/${val2}/`, 'GET', option).json()).body
})
}
}

export type ApiInstance = ReturnType<typeof api>
export default api
21 changes: 21 additions & 0 deletions packages/aspida/sample2/v1.1/3.1/$api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable */
import { AspidaClient } from 'aspida'
import { Methods as Methods0 } from './index'

const api = <T>(client: AspidaClient<T>) => {
const prefix = `${(client.baseURL === undefined ? '' : client.baseURL).replace(/\/$/, '')}/v1.1/3.1`

return {
get: (option: { query?: Methods0['get']['query'], headers: Methods0['get']['reqHeaders'], config?: T }) =>
client.fetch<Methods0['get']['resBody']>(prefix, '/', 'GET', option).json(),
$get: async (option: { query?: Methods0['get']['query'], headers: Methods0['get']['reqHeaders'], config?: T }) =>
(await client.fetch<Methods0['get']['resBody']>(prefix, '/', 'GET', option).json()).body,
post: (option: { body?: Methods0['post']['reqBody'], query: Methods0['post']['query'], config?: T }) =>
client.fetch<Methods0['post']['resBody']>(prefix, '/', 'POST', option, 'URLSearchParams').json(),
$post: async (option: { body?: Methods0['post']['reqBody'], query: Methods0['post']['query'], config?: T }) =>
(await client.fetch<Methods0['post']['resBody']>(prefix, '/', 'POST', option, 'URLSearchParams').json()).body
}
}

export type ApiInstance = ReturnType<typeof api>
export default api
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions packages/aspida/sample2/v1.1/users/$api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint-disable */
import { AspidaClient } from 'aspida'
import * as ApiTypes from '../../@types'
import { Methods as Methods0 } from './_userId@User[\'id\']'

const api = <T>(client: AspidaClient<T>) => {
const prefix = `${(client.baseURL === undefined ? '' : client.baseURL).replace(/\/$/, '')}/v1.1/users`

return {
_userId: (val0: ApiTypes.User['id']) => ({
get: (option: { query: Methods0['get']['query'], headers: Methods0['get']['reqHeaders'], config?: T }) =>
client.fetch<Methods0['get']['resBody']>(prefix, `/${val0}/`, 'GET', option).json(),
$get: async (option: { query: Methods0['get']['query'], headers: Methods0['get']['reqHeaders'], config?: T }) =>
(await client.fetch<Methods0['get']['resBody']>(prefix, `/${val0}/`, 'GET', option).json()).body,
post: (option: { query: Methods0['post']['query'], config?: T }) =>
client.fetch<Methods0['post']['resBody']>(prefix, `/${val0}/`, 'POST', option).json(),
$post: async (option: { query: Methods0['post']['query'], config?: T }) =>
(await client.fetch<Methods0['post']['resBody']>(prefix, `/${val0}/`, 'POST', option).json()).body
})
}
}

export type ApiInstance = ReturnType<typeof api>
export default api

0 comments on commit 6122a21

Please sign in to comment.