Skip to content

Commit a43d5e5

Browse files
committed
Add typing test
1 parent 13b485b commit a43d5e5

File tree

3 files changed

+44
-48
lines changed

3 files changed

+44
-48
lines changed

packages/fetch/src/__tests__/api.test.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,7 @@ test('onError middleware is called when onResponse is not defined and error exis
423423
const api = new DevupApi('https://api.example.com', undefined, 'openapi.json')
424424
let errorMiddlewareCalled = false
425425

426-
// onError is only called when onResponse is not defined and error exists
427-
// The condition is: if (response && middleware.onResponse) - if onResponse is not defined, the block doesn't execute
428-
// So onError is never called in the current implementation
429-
// This test verifies the middleware structure exists
426+
// onError is called when there's an error and no onResponse handler returned a result
430427
api.use({
431428
onError: async ({ error }) => {
432429
errorMiddlewareCalled = true
@@ -437,9 +434,8 @@ test('onError middleware is called when onResponse is not defined and error exis
437434

438435
await api.get('/test' as never)
439436

440-
// Note: onError is not called because the condition requires response && middleware.onResponse
441-
// If onResponse is not defined, the entire block is skipped
442-
expect(errorMiddlewareCalled).toBe(false)
437+
// onError should be called when there's an error response (404)
438+
expect(errorMiddlewareCalled).toBe(true)
443439
})
444440

445441
test('onError middleware can return Error', async () => {
@@ -455,7 +451,7 @@ test('onError middleware can return Error', async () => {
455451
const api = new DevupApi('https://api.example.com', undefined, 'openapi.json')
456452
const customError = new Error('Custom error from middleware')
457453

458-
// onError is registered but won't be called due to the condition check
454+
// onError returns a custom Error to replace the original error
459455
api.use({
460456
onError: async () => customError,
461457
})
@@ -466,9 +462,8 @@ test('onError middleware can return Error', async () => {
466462
response: Response
467463
}
468464

469-
// Since onError is not called, error comes from convertResponse
470-
expect(result.error).toBeDefined()
471-
expect(result.error).not.toBe(customError)
465+
// onError is called and returns the custom error
466+
expect(result.error).toBe(customError)
472467
})
473468

474469
test('onError middleware can return Response', async () => {
@@ -487,7 +482,7 @@ test('onError middleware can return Response', async () => {
487482
headers: { 'Content-Type': 'application/json' },
488483
})
489484

490-
// onError is registered but won't be called due to the condition check
485+
// onError returns a recovery Response to replace the error
491486
api.use({
492487
onError: async () => recoveryResponse,
493488
})
@@ -498,9 +493,8 @@ test('onError middleware can return Response', async () => {
498493
response: Response
499494
}
500495

501-
// Since onError is not called, response comes from convertResponse
502-
expect(result.response).toBeDefined()
503-
expect(result.response).not.toBe(recoveryResponse)
496+
// onError is called and returns the recovery response
497+
expect(result.response).toBe(recoveryResponse)
504498
})
505499

506500
test('middleware can be passed in request options', async () => {

packages/fetch/src/api.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -292,36 +292,41 @@ export class DevupApi<S extends ConditionalKeys<DevupApiServers>> {
292292
let error: unknown = ret.error
293293

294294
for (const middleware of finalMiddleware) {
295-
if (response && middleware.onResponse) {
296-
const result = await (response && middleware.onResponse
297-
? middleware.onResponse({
298-
request,
299-
schemaPath: url,
300-
params: requestOptions.params,
301-
query: requestOptions.query,
302-
headers: requestOptions.headers,
303-
body: requestOptions.body,
304-
response: ret.response,
305-
})
306-
: error && middleware.onError
307-
? middleware.onError({
308-
request,
309-
schemaPath: url,
310-
params: requestOptions.params,
311-
query: requestOptions.query,
312-
headers: requestOptions.headers,
313-
body: requestOptions.body,
314-
error: ret.error,
315-
})
316-
: undefined)
317-
if (result) {
318-
if (result instanceof Response) {
319-
response = result
320-
break
321-
} else if (result instanceof Error) {
322-
error = result
323-
break
324-
}
295+
const middlewareParams = {
296+
request,
297+
schemaPath: url,
298+
params: requestOptions.params,
299+
query: requestOptions.query,
300+
headers: requestOptions.headers,
301+
body: requestOptions.body,
302+
}
303+
304+
let result: Response | Error | undefined
305+
306+
// Call onResponse if it exists
307+
if (middleware.onResponse) {
308+
result = await middleware.onResponse({
309+
...middlewareParams,
310+
response: ret.response,
311+
})
312+
}
313+
314+
// Call onError if there's an error and onResponse didn't return a result
315+
if (!result && error && middleware.onError) {
316+
result = await middleware.onError({
317+
...middlewareParams,
318+
error: ret.error,
319+
})
320+
}
321+
322+
if (result) {
323+
if (result instanceof Response) {
324+
response = result
325+
break
326+
}
327+
if (result instanceof Error) {
328+
error = result
329+
break
325330
}
326331
}
327332
}

packages/generator/src/generate-schema.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ export function getTypeFromSchema(
9191

9292
// Handle primitive types
9393
if (schemaObj.type === 'string') {
94-
if (schemaObj.format === 'date' || schemaObj.format === 'date-time') {
95-
return { type: 'string', default: schemaObj.default }
96-
}
9794
return { type: 'string', default: schemaObj.default }
9895
}
9996

0 commit comments

Comments
 (0)