Skip to content

Commit

Permalink
feat: assign strings to variable for uglify
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Jun 25, 2020
1 parent f0ea3f2 commit 5b92af1
Show file tree
Hide file tree
Showing 10 changed files with 596 additions and 437 deletions.
262 changes: 148 additions & 114 deletions packages/aspida/sample1/$api.ts

Large diffs are not rendered by default.

263 changes: 149 additions & 114 deletions packages/aspida/sample2/$api.ts

Large diffs are not rendered by default.

215 changes: 122 additions & 93 deletions packages/aspida/sample2/v1.1/$api.ts

Large diffs are not rendered by default.

156 changes: 88 additions & 68 deletions packages/aspida/sample2/v1.1/2/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,78 +7,98 @@ 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`
const GET = 'GET'
const POST = 'POST'
const PUT = 'PUT'
const DELETE = 'DELETE'
const PATH0 = '/entries.json/'
const PATH1 = '/test-4/'
const PATH2 = '/'
const PATH3 = '/test-4/fuga aa/'
const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const prefix = `${(baseURL === undefined ? '' : 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
_hogeId_0: (val0: ApiTypes.HogeId) => {
const prefix0 = `${prefix}/${val0}`

return {
entries_json: {
get: (option?: { config?: T }) =>
fetch<Methods0['get']['resBody']>(prefix0, PATH0, GET, option).json(),
$get: (option?: { config?: T }) =>
fetch<Methods0['get']['resBody']>(prefix0, PATH0, GET, option).json().then(r => r.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
test_4: {
_fugaId: (val1: number | string) => {
const prefix1 = `${prefix0}/${val1}`

return {
get: (option?: { query?: Methods2['get']['query'], config?: T }) =>
fetch<Methods2['get']['resBody']>(prefix1, PATH2, GET, option).json(),
$get: (option?: { query?: Methods2['get']['query'], config?: T }) =>
fetch<Methods2['get']['resBody']>(prefix1, PATH2, GET, option).json().then(r => r.body),
post: (option: { body?: Methods2['post']['reqBody'], query: Methods2['post']['query'], config?: T }) =>
fetch<Methods2['post']['resBody']>(prefix1, PATH2, POST, option).json(),
$post: (option: { body?: Methods2['post']['reqBody'], query: Methods2['post']['query'], config?: T }) =>
fetch<Methods2['post']['resBody']>(prefix1, PATH2, POST, option).json().then(r => r.body),
put: (option: { query: Methods2['put']['query'], config?: T }) =>
fetch<Methods2['put']['resBody']>(prefix1, PATH2, PUT, option).json(),
$put: (option: { query: Methods2['put']['query'], config?: T }) =>
fetch<Methods2['put']['resBody']>(prefix1, PATH2, PUT, option).json().then(r => r.body),
delete: (option: { query: Methods2['delete']['query'], config?: T }) =>
fetch<Methods2['delete']['resBody']>(prefix1, PATH2, DELETE, option).json(),
$delete: (option: { query: Methods2['delete']['query'], config?: T }) =>
fetch<Methods2['delete']['resBody']>(prefix1, PATH2, DELETE, option).json().then(r => r.body)
}
},
fuga_aa: {
get: (option: { query: Methods3['get']['query'], config?: T }) =>
fetch<Methods3['get']['resBody']>(prefix0, PATH3, GET, option).json(),
$get: (option: { query: Methods3['get']['query'], config?: T }) =>
fetch<Methods3['get']['resBody']>(prefix0, PATH3, GET, option).json().then(r => r.body),
post: (option: { body?: Methods3['post']['reqBody'], query: Methods3['post']['query'], config?: T }) =>
fetch<Methods3['post']['resBody']>(prefix0, PATH3, POST, option).json(),
$post: (option: { body?: Methods3['post']['reqBody'], query: Methods3['post']['query'], config?: T }) =>
fetch<Methods3['post']['resBody']>(prefix0, PATH3, POST, option).json().then(r => r.body),
put: (option: { query: Methods3['put']['query'], config?: T }) =>
fetch<Methods3['put']['resBody']>(prefix0, PATH3, PUT, option).json(),
$put: (option: { query: Methods3['put']['query'], config?: T }) =>
fetch<Methods3['put']['resBody']>(prefix0, PATH3, PUT, option).json().then(r => r.body),
delete: (option: { body: Methods3['delete']['reqBody'], query: Methods3['delete']['query'], config?: T }) =>
fetch<Methods3['delete']['resBody']>(prefix0, PATH3, DELETE, option).json(),
$delete: (option: { body: Methods3['delete']['reqBody'], query: Methods3['delete']['query'], config?: T }) =>
fetch<Methods3['delete']['resBody']>(prefix0, PATH3, DELETE, option).json().then(r => r.body)
},
get: (option: { query: Methods1['get']['query'], config?: T }) =>
fetch<void>(prefix0, PATH1, GET, option).send(),
$get: (option: { query: Methods1['get']['query'], config?: T }) =>
fetch<void>(prefix0, PATH1, GET, option).send().then(r => r.body),
post: (option?: { body?: Methods1['post']['reqBody'], query?: Methods1['post']['query'], config?: T }) =>
fetch<void>(prefix0, PATH1, POST, option).send(),
$post: (option?: { body?: Methods1['post']['reqBody'], query?: Methods1['post']['query'], config?: T }) =>
fetch<void>(prefix0, PATH1, POST, option).send().then(r => r.body),
put: (option?: { query?: Methods1['put']['query'], config?: T }) =>
fetch<Methods1['put']['resBody']>(prefix0, PATH1, PUT, option).json(),
$put: (option?: { query?: Methods1['put']['query'], config?: T }) =>
fetch<Methods1['put']['resBody']>(prefix0, PATH1, PUT, option).json().then(r => r.body),
delete: (option: { query: Methods1['delete']['query'], config?: T }) =>
fetch<Methods1['delete']['resBody']>(prefix0, PATH1, DELETE, option).json(),
$delete: (option: { query: Methods1['delete']['query'], config?: T }) =>
fetch<Methods1['delete']['resBody']>(prefix0, PATH1, DELETE, option).json().then(r => r.body)
}
}
},
_hogeId_1: (val2: number) => {
const prefix2 = `${prefix}/${val2}`

return {
get: (option: { query?: Methods4['get']['query'], headers: Methods4['get']['reqHeaders'], config?: T }) =>
fetch<Methods4['get']['resBody']>(prefix2, PATH2, GET, option).json(),
$get: (option: { query?: Methods4['get']['query'], headers: Methods4['get']['reqHeaders'], config?: T }) =>
fetch<Methods4['get']['resBody']>(prefix2, PATH2, GET, option).json().then(r => r.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
})
}
}
}

Expand Down
19 changes: 11 additions & 8 deletions packages/aspida/sample2/v1.1/3.1/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
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`
const GET = 'GET'
const POST = 'POST'
const PATH0 = '/'
const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const prefix = `${(baseURL === undefined ? '' : 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,
fetch<Methods0['get']['resBody']>(prefix, PATH0, GET, option).json(),
$get: (option: { query?: Methods0['get']['query'], headers: Methods0['get']['reqHeaders'], config?: T }) =>
fetch<Methods0['get']['resBody']>(prefix, PATH0, GET, option).json().then(r => r.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
fetch<Methods0['post']['resBody']>(prefix, PATH0, POST, option, 'URLSearchParams').json(),
$post: (option: { body?: Methods0['post']['reqBody'], query: Methods0['post']['query'], config?: T }) =>
fetch<Methods0['post']['resBody']>(prefix, PATH0, POST, option, 'URLSearchParams').json().then(r => r.body)
}
}

Expand Down
31 changes: 19 additions & 12 deletions packages/aspida/sample2/v1.1/users/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@ 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`
const GET = 'GET'
const POST = 'POST'
const PATH0 = '/'
const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const prefix = `${(baseURL === undefined ? '' : 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
})
_userId: (val0: ApiTypes.User['id']) => {
const prefix0 = `${prefix}/${val0}`

return {
get: (option: { query: Methods0['get']['query'], headers: Methods0['get']['reqHeaders'], config?: T }) =>
fetch<Methods0['get']['resBody']>(prefix0, PATH0, GET, option).json(),
$get: (option: { query: Methods0['get']['query'], headers: Methods0['get']['reqHeaders'], config?: T }) =>
fetch<Methods0['get']['resBody']>(prefix0, PATH0, GET, option).json().then(r => r.body),
post: (option: { query: Methods0['post']['query'], config?: T }) =>
fetch<Methods0['post']['resBody']>(prefix0, PATH0, POST, option).json(),
$post: (option: { query: Methods0['post']['query'], config?: T }) =>
fetch<Methods0['post']['resBody']>(prefix0, PATH0, POST, option).json().then(r => r.body)
}
}
}
}

Expand Down
12 changes: 7 additions & 5 deletions packages/aspida/sample2/v2.0/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
import { AspidaClient } from 'aspida'
import { Methods as Methods0 } from './index'

const api = <T>(client: AspidaClient<T>) => {
const prefix = `${(client.baseURL === undefined ? '' : client.baseURL).replace(/\/$/, '')}/v2.0`
const GET = 'GET'
const PATH0 = '/'
const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const prefix = `${(baseURL === undefined ? '' : baseURL).replace(/\/$/, '')}/v2.0`

return {
get: (option: { query: Methods0['get']['query'], headers: Methods0['get']['reqHeaders'], config?: T }) =>
client.fetch<Methods0['get']['resBody'], Methods0['get']['resHeaders'], Methods0['get']['status']>(prefix, '/', 'GET', option).text(),
$get: async (option: { query: Methods0['get']['query'], headers: Methods0['get']['reqHeaders'], config?: T }) =>
(await client.fetch<Methods0['get']['resBody'], Methods0['get']['resHeaders'], Methods0['get']['status']>(prefix, '/', 'GET', option).text()).body
fetch<Methods0['get']['resBody'], Methods0['get']['resHeaders'], Methods0['get']['status']>(prefix, PATH0, GET, option).text(),
$get: (option: { query: Methods0['get']['query'], headers: Methods0['get']['reqHeaders'], config?: T }) =>
fetch<Methods0['get']['resBody'], Methods0['get']['resHeaders'], Methods0['get']['status']>(prefix, PATH0, GET, option).text().then(r => r.body)
}
}

Expand Down
11 changes: 7 additions & 4 deletions packages/aspida/src/buildTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ const createTemplate = (
trailingSlash: boolean,
appendPrefix?: string
) => {
const { api, imports } = createTemplateValues(tree, trailingSlash)
const { api, imports, pathes } = createTemplateValues(tree, trailingSlash)
const text = `/* eslint-disable */
import { AspidaClient${api.includes('BasicHeaders') ? ', BasicHeaders' : ''} } from 'aspida'
<% types %><% imports %>
const api = <T>(client: AspidaClient<T>) => {
${['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'PATCH', 'OPTIONS']
.filter(m => api.includes(`, ${m}, option`))
.map(m => `\nconst ${m} = '${m}'`)
.join('')}${pathes.map((p, i) => `\nconst PATH${i} = ${p}`).join('')}
const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const prefix = ${
appendPrefix ? '`${' : ''
}(client.baseURL === undefined ? '<% baseURL %>' : client.baseURL).replace(/\\/$/, '')${
}(baseURL === undefined ? '<% baseURL %>' : baseURL).replace(/\\/$/, '')${
appendPrefix ? `}/${appendPrefix}\`` : ''
}
Expand Down
15 changes: 6 additions & 9 deletions packages/aspida/src/createMethodsString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export default (
methods: Method[],
indent: string,
importName: string,
newUrl: string,
trailingSlash: boolean
newPrefix: string,
path: string
) =>
methods
.map(({ name, props }) => {
Expand Down Expand Up @@ -60,19 +60,16 @@ export default (
[key: string]: string
})[props.resBody.value] || 'json'

const quotation = newUrl.includes('${') ? '`' : "'"
const tmpChanks = [
`(${option(name)}) =>`,
`client.fetch<${resBody(name)}${resHeaders(name)}${status(
`fetch<${resBody(name)}${resHeaders(name)}${status(
name
)}>(prefix, ${quotation}${newUrl}${
trailingSlash ? '/' : ''
}${quotation}, '${name.toUpperCase()}'${request()}).${resMethodName()}()`
)}>(${newPrefix}, ${path}, ${name.toUpperCase()}${request()}).${resMethodName()}()`
]

return `${indent} ${name}: ${tmpChanks[0]}
${indent} ${tmpChanks[1]},
${indent} $${name}: async ${tmpChanks[0]}
${indent} (await ${tmpChanks[1]}).body`
${indent} $${name}: ${tmpChanks[0]}
${indent} ${tmpChanks[1]}.then(r => r.body)`
})
.join(',\n')

0 comments on commit 5b92af1

Please sign in to comment.