Skip to content

Commit e5d0d98

Browse files
committed
refactor: split core into multiple files
1 parent c80d259 commit e5d0d98

9 files changed

Lines changed: 555 additions & 509 deletions

File tree

playground/nuxt.config.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { fileURLToPath } from 'node:url'
22
import MyModule from '../src/module'
33

44
export default defineNuxtConfig({
5-
modules : ['@privyid/nhp', MyModule],
6-
alias : { '@privyid/nuapi/core': fileURLToPath(new URL('../src/core/', import.meta.url)) },
7-
nuapi : {},
8-
typescript: {
5+
compatibilityDate: '2024-10-11',
6+
modules : ['@privyid/nhp', MyModule],
7+
alias : { '@privyid/nuapi/core': fileURLToPath(new URL('../src/core/', import.meta.url)) },
8+
nuapi : {},
9+
typescript : {
910
tsConfig: {
1011
compilerOptions: {
1112
strict : false,

src/core/global.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {
2+
describe,
3+
expect,
4+
it,
5+
} from 'vitest'
6+
import {
7+
setApi,
8+
useApi,
9+
} from './global'
10+
import { createApi } from './instance'
11+
12+
describe('useApi', () => {
13+
it('should return same instance and same instance (singleton)', () => {
14+
const a = useApi()
15+
const b = useApi()
16+
17+
expect(a).toStrictEqual(b)
18+
})
19+
})
20+
21+
describe('setApi', () => {
22+
it('should be able to replace global instance', () => {
23+
const old = useApi()
24+
const fresh = createApi()
25+
26+
setApi(fresh)
27+
28+
const last = useApi()
29+
30+
expect(last).toStrictEqual(fresh)
31+
expect(last).not.toStrictEqual(old)
32+
})
33+
})

src/core/global.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { createLazyton } from './instance'
2+
3+
/**
4+
* Use global api instance
5+
* @example
6+
* let api = useApi()
7+
*
8+
* api.get('/some/endpoint')
9+
*/
10+
export const useApi = createLazyton({}, true)
11+
12+
/**
13+
* Set global api instance
14+
* @param instance
15+
*/
16+
export const setApi = useApi.setApi
Lines changed: 7 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,23 @@
1-
import { type AxiosResponse } from 'axios'
21
import {
32
describe,
4-
it,
53
expect,
4+
it,
65
vi as jest,
76
} from 'vitest'
7+
import { useApi } from './global'
88
import {
9-
type RequestHook,
10-
type ResponseHook,
11-
createApi,
12-
createLazyton,
9+
copyHook,
1310
onError,
1411
onRequest,
1512
onRequestError,
1613
onResponse,
1714
onResponseError,
1815
removeHook,
1916
resetHook,
20-
setApi,
21-
useApi,
22-
copyHook,
23-
QueuePriority,
24-
} from '.'
25-
26-
describe('useApi', () => {
27-
it('should return same instance and same instance (singleton)', () => {
28-
const a = useApi()
29-
const b = useApi()
30-
31-
expect(a).toStrictEqual(b)
32-
})
33-
})
34-
35-
describe('setApi', () => {
36-
it('should be able to replace global instance', () => {
37-
const old = useApi()
38-
const fresh = createApi()
39-
40-
setApi(fresh)
41-
42-
const last = useApi()
43-
44-
expect(last).toStrictEqual(fresh)
45-
expect(last).not.toStrictEqual(old)
46-
})
47-
})
17+
} from './hooks'
18+
import type { AxiosResponse } from 'axios'
19+
import type { RequestHook, ResponseHook } from './types'
20+
import { createApi } from './instance'
4821

4922
describe('Hooks utils', () => {
5023
it('should be able to registering on-request hook using onRequest()', async () => {
@@ -178,104 +151,3 @@ describe('copyHook', () => {
178151
expect(fn).toBeCalledWith(expected)
179152
})
180153
})
181-
182-
describe('Inherit instance', () => {
183-
it('should be able to create new instance with same config', async () => {
184-
const a = createApi({ baseURL: `${process.env.BASE_URL as string}/v1`, headers: { foo: 'bar' } })
185-
const b = a.create({ baseURL: `${process.env.BASE_URL as string}/v2` })
186-
187-
expect(b).not.toStrictEqual(a)
188-
189-
const response = await b.get('/api/ping')
190-
191-
expect(b.parent).toBe(a)
192-
expect(response.config.headers?.foo).toBe('bar')
193-
expect(response.data.data).toStrictEqual({ version: 'v2' })
194-
})
195-
196-
it('should be copy hook to new instance', async () => {
197-
const a = createApi({ baseURL: `${process.env.BASE_URL as string}/v1`, headers: { foo: 'bar' } })
198-
const fn = jest.fn((config) => config)
199-
200-
onRequest(fn, a)
201-
202-
const b = a.create({ baseURL: `${process.env.BASE_URL as string}/v2` })
203-
const expected = expect.objectContaining({
204-
baseURL: `${process.env.BASE_URL as string}/v2`,
205-
headers: expect.objectContaining({ foo: 'bar' }),
206-
})
207-
208-
await b.get('/api/ping')
209-
210-
expect(fn).toBeCalled()
211-
expect(fn).toBeCalledWith(expected)
212-
})
213-
214-
it('should prefixing baseUrl if prefixURL is present', async () => {
215-
const a = createApi({ baseURL: process.env.BASE_URL, headers: { foo: 'bar' } })
216-
const b = a.create({ prefixURL: 'api' })
217-
const response = await b.get('user')
218-
219-
expect(response.status).toBe(200)
220-
expect(response.data.data).toBe('data-user')
221-
})
222-
223-
it('should share queue in same parent instance', async () => {
224-
const a = createApi({ baseURL: process.env.BASE_URL, queue: { worker: 1 } })
225-
const b = a.create({ prefixURL: 'v1/api' })
226-
const c = a.create({ prefixURL: 'v2/api' })
227-
228-
const result: number[] = []
229-
230-
await Promise.all([
231-
a.get('api/ping').then(() => { result.push(99) }),
232-
b.get('ping').then(() => { result.push(1) }),
233-
b.get('ping', { priority: QueuePriority.LOW }).then(() => { result.push(2) }),
234-
c.get('ping').then(() => { result.push(3) }),
235-
c.get('ping', { priority: QueuePriority.HIGH }).then(() => { result.push(4) }),
236-
])
237-
238-
expect(result).toStrictEqual([
239-
4,
240-
99,
241-
1,
242-
3,
243-
2,
244-
])
245-
})
246-
})
247-
248-
describe('lazyton', () => {
249-
it('should create lazy instance', () => {
250-
const useLazy = createLazyton({ baseURL: '/v1' })
251-
252-
const a = useLazy()
253-
const b = useLazy()
254-
255-
expect(typeof useLazy).toBe('function')
256-
expect(a).toStrictEqual(b)
257-
})
258-
259-
it('should inherit hook from global', async () => {
260-
const useLazy = createLazyton({ prefixURL: '/api' })
261-
const fn = jest.fn((config) => config)
262-
263-
onRequest(fn)
264-
265-
await useLazy().get('/ping')
266-
267-
expect(fn).toBeCalled()
268-
})
269-
270-
it('should create fresh instace if parameter fresh set to true', async () => {
271-
const useLazy = createLazyton({ baseURL: `${process.env.BASE_URL as string}/api` }, true)
272-
const fn = jest.fn((config) => config)
273-
const a = useLazy()
274-
275-
onRequest(fn)
276-
277-
await a.get('/ping')
278-
279-
expect(fn).not.toBeCalled()
280-
})
281-
})

0 commit comments

Comments
 (0)