Skip to content

Commit 0a2b822

Browse files
committed
linting
1 parent 4b11081 commit 0a2b822

10 files changed

Lines changed: 172 additions & 152 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ node_modules
88
stats.html
99
.DS_Store
1010
coverage
11+
generated

open-api-configuration/api.ts

Lines changed: 77 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@ type ServerMap = {
1414
const operationServerMap: ServerMap = {}
1515

1616
function setFlattenedQueryParams(urlSearchParams: URLSearchParams, parameter: any, key = ''): void {
17-
if (parameter == null) return
18-
if (typeof parameter === 'object') {
19-
if (Array.isArray(parameter)) {
20-
;(parameter as any[]).forEach(item => setFlattenedQueryParams(urlSearchParams, item, key))
21-
} else {
22-
Object.keys(parameter).forEach(currentKey =>
23-
setFlattenedQueryParams(
24-
urlSearchParams,
25-
parameter[currentKey],
26-
`${key}${key !== '' ? '.' : ''}${currentKey}`
27-
)
28-
)
29-
}
17+
if (parameter == null) return
18+
if (typeof parameter === 'object') {
19+
if (Array.isArray(parameter)) {
20+
;(parameter as any[]).forEach(item => setFlattenedQueryParams(urlSearchParams, item, key))
3021
} else {
31-
if (urlSearchParams.has(key)) urlSearchParams.append(key, parameter)
32-
else urlSearchParams.set(key, parameter)
22+
Object.keys(parameter).forEach(currentKey =>
23+
setFlattenedQueryParams(
24+
urlSearchParams,
25+
parameter[currentKey],
26+
`${key}${key !== '' ? '.' : ''}${currentKey}`
27+
)
28+
)
3329
}
30+
} else {
31+
if (urlSearchParams.has(key)) urlSearchParams.append(key, parameter)
32+
else urlSearchParams.set(key, parameter)
33+
}
3434
}
3535

3636
const setSearchParams = (url: URL, ...objects: any[]) => {
37-
const searchParams = new URLSearchParams(url.search)
38-
setFlattenedQueryParams(searchParams, objects)
39-
url.search = searchParams.toString()
37+
const searchParams = new URLSearchParams(url.search)
38+
setFlattenedQueryParams(searchParams, objects)
39+
url.search = searchParams.toString()
4040
}
4141

4242
const toPathString = (url: URL) => url.pathname + url.search + url.hash
@@ -47,80 +47,82 @@ const createRequestFunction = (
4747
BASE_PATH: string,
4848
configuration?: Configuration
4949
) => {
50-
return <T = unknown, R = AxiosResponse<T>>(
51-
axios: AxiosInstance = globalAxiosInst,
52-
basePath: string = BASE_PATH
53-
) => {
54-
const axiosRequestArgs = {
55-
...axiosArgs.options,
56-
url: (axios.defaults.baseURL ? '' : configuration?.basePath ?? basePath) + axiosArgs.url
57-
}
58-
return axios.request<T, R>(axiosRequestArgs)
50+
return <T = unknown, R = AxiosResponse<T>>(
51+
axios: AxiosInstance = globalAxiosInst,
52+
basePath: string = BASE_PATH
53+
) => {
54+
const axiosRequestArgs = {
55+
...axiosArgs.options,
56+
url: (axios.defaults.baseURL ? '' : configuration?.basePath ?? basePath) + axiosArgs.url,
5957
}
58+
return axios.request<T, R>(axiosRequestArgs)
59+
}
6060
}
6161

6262
class BaseAPI {
63-
protected configuration: Configuration | undefined
63+
protected configuration: Configuration | undefined
6464

65-
constructor(
66-
configuration?: Configuration,
65+
constructor(
66+
configuration?: Configuration,
6767
protected basePath: string = 'http://example',
6868
protected axios: AxiosInstance = globalAxios
69-
) {
70-
if (configuration) {
71-
this.configuration = configuration
72-
this.basePath = configuration.basePath ?? basePath
73-
}
69+
) {
70+
if (configuration) {
71+
this.configuration = configuration
72+
this.basePath = configuration.basePath ?? basePath
7473
}
74+
}
7575
}
7676

7777
export const DefaultApiAxiosParamCreator = (configuration?: Configuration) => ({
78-
fetchData: async (options: RawAxiosRequestConfig = {}): Promise<RequestArgs> => {
79-
const localVarPath = `/data`
80-
const localVarUrlObj = new URL(localVarPath, 'http://example')
81-
let baseOptions
82-
if (configuration) baseOptions = configuration.baseOptions
83-
84-
const localVarRequestOptions = { method: 'GET', ...baseOptions, ...options }
85-
const localVarHeaderParameter = {} as any
86-
const localVarQueryParameter = {} as any
87-
88-
setSearchParams(localVarUrlObj, localVarQueryParameter)
89-
const headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {}
90-
localVarRequestOptions.headers = {
91-
...localVarHeaderParameter,
92-
...headersFromBaseOptions,
93-
...options.headers
94-
}
95-
96-
return {
97-
url: toPathString(localVarUrlObj),
98-
options: localVarRequestOptions
99-
}
78+
fetchData: async (options: RawAxiosRequestConfig = {}): Promise<RequestArgs> => {
79+
const localVarPath = `/data`
80+
const localVarUrlObj = new URL(localVarPath, 'http://example')
81+
let baseOptions
82+
if (configuration) baseOptions = configuration.baseOptions
83+
84+
const localVarRequestOptions = { method: 'GET', ...baseOptions, ...options }
85+
const localVarHeaderParameter = {} as any
86+
const localVarQueryParameter = {} as any
87+
88+
setSearchParams(localVarUrlObj, localVarQueryParameter)
89+
const headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {}
90+
localVarRequestOptions.headers = {
91+
...localVarHeaderParameter,
92+
...headersFromBaseOptions,
93+
...options.headers,
94+
}
95+
96+
return {
97+
url: toPathString(localVarUrlObj),
98+
options: localVarRequestOptions,
10099
}
100+
},
101101
})
102102

103103
export const DefaultApiFp = (configuration?: Configuration) => {
104-
const localVarAxiosParamCreator = DefaultApiAxiosParamCreator(configuration)
105-
return {
106-
async fetchData(
107-
options?: RawAxiosRequestConfig
108-
): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<string>> {
109-
const localVarAxiosArgs = await localVarAxiosParamCreator.fetchData(options)
110-
const localVarOperationServerIndex = configuration?.serverIndex ?? 0
111-
const localVarOperationServerBasePath =
104+
const localVarAxiosParamCreator = DefaultApiAxiosParamCreator(configuration)
105+
return {
106+
async fetchData(
107+
options?: RawAxiosRequestConfig
108+
): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<string>> {
109+
const localVarAxiosArgs = await localVarAxiosParamCreator.fetchData(options)
110+
const localVarOperationServerIndex = configuration?.serverIndex ?? 0
111+
const localVarOperationServerBasePath =
112112
operationServerMap['DefaultApi.fetchData']?.[localVarOperationServerIndex]?.url
113-
return (axios, basePath) =>
114-
createRequestFunction(localVarAxiosArgs, globalAxios, 'http://example', configuration)(
115-
axios,
116-
localVarOperationServerBasePath || basePath
117-
)
118-
}
119-
}
113+
return (axios, basePath) =>
114+
createRequestFunction(localVarAxiosArgs, globalAxios, 'http://example', configuration)(
115+
axios,
116+
localVarOperationServerBasePath || basePath
117+
)
118+
},
119+
}
120120
}
121121

122+
export type DefaultApiFp = typeof DefaultApiFp
123+
122124
export class DefaultApi extends BaseAPI {
123-
fetchData(options?: RawAxiosRequestConfig) {
124-
return DefaultApiFp(this.configuration).fetchData(options).then(request => request(this.axios, this.basePath))
125-
}
125+
fetchData(options?: RawAxiosRequestConfig) {
126+
return DefaultApiFp(this.configuration).fetchData(options).then(request => request(this.axios, this.basePath))
127+
}
126128
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "react-openapi-generator-hook",
3-
"version": "0.3.0-experimental",
3+
"version": "0.3.2-experimental",
44
"main": "dist/react-openapi-hook.umd.js",
55
"module": "dist/react-openapi-hook.es.js",
66
"types": "dist/index.d.ts",
77
"scripts": {
88
"build": "vite build",
9-
"lint": "eslint .",
10-
"lint:fix": "eslint . --fix",
9+
"lint": "eslint ./src",
10+
"lint:fix": "eslint ./src --fix",
1111
"generate:apis": "openapi-generator-cli generate -i openapi-specs.yaml -g typescript-axios --additional-properties=useSingleRequestParameter=true --skip-validate-spec -o ./generated",
1212
"coverage": "vitest run --coverage --silent"
1313
},

src/hook/useApi.test.tsx

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import { createOpenApiTestWrapper } from '../../test/test-wrappers'
99
import { type AxiosError } from 'axios'
1010
import { Configuration } from '../../open-api-configuration/configuration'
1111

12+
type MethodName = 'fetchData'
13+
type Data = { ok: boolean }
14+
type ApiMethod = (params?: unknown, options?: unknown) => Promise<{ data: Data }>
15+
type MockApiType = Record<MethodName, ApiMethod>
16+
1217
describe('useApi', () => {
1318
const apiFactory = vi.fn()
1419
const methodName = 'testMethod'
@@ -25,7 +30,7 @@ describe('useApi', () => {
2530
requestParameters,
2631
requestOptions,
2732
}),
28-
{ wrapper }
33+
{ wrapper }
2934
)
3035

3136
const [state] = result.current
@@ -46,7 +51,7 @@ describe('useApi', () => {
4651
requestParameters,
4752
requestOptions,
4853
}),
49-
{ wrapper }
54+
{ wrapper }
5055
)
5156

5257
const [, execute] = result.current
@@ -70,16 +75,17 @@ describe('useApi', () => {
7075
const method = vi.fn()
7176
apiFactory.mockReturnValue({ [methodName]: method })
7277

73-
renderHook(() =>
74-
useApi(
75-
{
76-
apiFactory,
77-
methodName,
78-
requestParameters,
79-
requestOptions,
80-
},
81-
{ manual: true }
82-
),
78+
renderHook(
79+
() =>
80+
useApi(
81+
{
82+
apiFactory,
83+
methodName,
84+
requestParameters,
85+
requestOptions,
86+
},
87+
{ manual: true }
88+
),
8389
{ wrapper }
8490
)
8591
expect(method).not.toHaveBeenCalled()
@@ -103,17 +109,17 @@ describe('useApi', () => {
103109

104110
it('sets error and rethrows on non-cancel error', async () => {
105111
// Fake API instance with a method that rejects
106-
const methodName = 'getThing' as const
112+
const methodName = 'fetchData' as const
107113
const apiFactory = vi.fn(() => ({
108114
[methodName]: vi.fn().mockRejectedValue(Object.assign(new Error('Boom'), {
109-
isAxiosError: true
110-
} satisfies Partial<AxiosError>))
115+
isAxiosError: true,
116+
} satisfies Partial<AxiosError>)),
111117
}))
112118
const wrapper = createOpenApiTestWrapper()
113119

114120
const { result } = renderHook(() =>
115-
useApi({ apiFactory, methodName }, { manual: true }),
116-
{ wrapper }
121+
useApi({ apiFactory, methodName }, { manual: true }),
122+
{ wrapper }
117123
)
118124

119125
const [, execute] = result.current
@@ -130,15 +136,17 @@ describe('useApi', () => {
130136
})
131137

132138
it('uses the secondary configuration when specified', async () => {
133-
const methodName = 'getSomething' as const
134-
const wrapper = createOpenApiTestWrapper({configurationId: undefined, defaultConfigurationId: 'secondaryConfiguration'})
139+
const methodName = 'fetchData' as const
140+
const wrapper =
141+
createOpenApiTestWrapper({ configurationId: undefined, defaultConfigurationId: 'secondaryConfiguration' })
135142

136143
const method = vi.fn().mockResolvedValue({ data: { ok: true } })
137-
const apiFactory = vi.fn((cfg?: Configuration, basePath?: string, axiosInstance?: any) => {
144+
const apiFactory = vi.fn((_cfg?: Configuration, basePath?: string, _axiosInstance?: unknown) => {
138145
expect(basePath).toBe('https://secondary.example')
139-
return { [methodName]: method } as any
146+
return { 'fetchData': method } satisfies MockApiType
140147
})
141-
renderHook(() =>
148+
renderHook(
149+
() =>
142150
useApi(
143151
{
144152
apiFactory,
@@ -152,17 +160,18 @@ describe('useApi', () => {
152160
)
153161
})
154162

155-
it('uses the first configuration in the map when no configurationId and no defaultConfigurationId specified', async () => {
156-
const methodName = 'getSomething' as const
157-
const wrapper = createOpenApiTestWrapper({configurationId: undefined, defaultConfigurationId: undefined})
158-
159-
const method = vi.fn().mockResolvedValue({ data: { ok: true } })
160-
const apiFactory = vi.fn((cfg?: Configuration, basePath?: string, axiosInstance?: any) => {
161-
expect(basePath).toBe('https://primary.example')
162-
return { [methodName]: method } as any
163-
})
164-
renderHook(() =>
165-
useApi(
163+
it('uses the first configuration in the map when no configurationId and no defaultConfigurationId specified',
164+
async () => {
165+
const methodName = 'fetchData' as const
166+
const wrapper = createOpenApiTestWrapper({ configurationId: undefined, defaultConfigurationId: undefined })
167+
168+
const method = vi.fn().mockResolvedValue({ data: { ok: true } })
169+
const apiFactory = vi.fn((_cfg?: Configuration, basePath?: string, _axiosInstance?: unknown) => {
170+
expect(basePath).toBe('https://primary.example')
171+
return { 'fetchData': method } satisfies MockApiType
172+
})
173+
renderHook(
174+
() => useApi(
166175
{
167176
apiFactory,
168177
methodName,
@@ -171,8 +180,7 @@ describe('useApi', () => {
171180
},
172181
{ manual: true }
173182
),
174-
{ wrapper }
175-
)
176-
})
177-
183+
{ wrapper }
184+
)
185+
})
178186
})

0 commit comments

Comments
 (0)