@@ -23,7 +23,13 @@ export type ApiFetchErrors<AdditionalErrors extends string = never> = {
23
23
errorType : ErrorTypes | AdditionalErrors
24
24
}
25
25
26
- type FetchInternalAPIOptionsWithSchema < T extends z . ZodType , AdditionalErrors > = {
26
+ type NonZodResponses = {
27
+ ArrayBuffer : ArrayBuffer
28
+ }
29
+
30
+ type ValidNonZodResponses = keyof NonZodResponses
31
+
32
+ type FetchInternalAPIOptionsWithSchema < T extends z . ZodType | ValidNonZodResponses , AdditionalErrors > = {
27
33
api : keyof typeof internalApis
28
34
path : `/${string } `
29
35
method : 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD'
@@ -33,7 +39,15 @@ type FetchInternalAPIOptionsWithSchema<T extends z.ZodType, AdditionalErrors> =
33
39
onApiError ?: ( response : Response ) => AdditionalErrors | undefined
34
40
}
35
41
36
- export async function fetchInternalAPI < Schema extends z . ZodType , AdditionalErrors extends string = never > ( {
42
+ export async function fetchInternalAPI <
43
+ Schema extends z . ZodType | ValidNonZodResponses ,
44
+ AdditionalErrors extends string = never ,
45
+ InferredReturnValue = Schema extends z . ZodType
46
+ ? z . infer < Schema >
47
+ : Schema extends ValidNonZodResponses
48
+ ? NonZodResponses [ Schema ]
49
+ : never ,
50
+ > ( {
37
51
api,
38
52
path,
39
53
headers,
@@ -42,7 +56,7 @@ export async function fetchInternalAPI<Schema extends z.ZodType, AdditionalError
42
56
responseSchema,
43
57
onApiError,
44
58
} : FetchInternalAPIOptionsWithSchema < Schema , AdditionalErrors > ) : Promise <
45
- z . infer < Schema > | ApiFetchErrors < AdditionalErrors >
59
+ InferredReturnValue | ApiFetchErrors < AdditionalErrors >
46
60
> {
47
61
const apiConfig = internalApis [ api ]
48
62
const scope = `api://dev-gcp.${ apiConfig . namespace } .${ api } /.default`
@@ -66,9 +80,9 @@ export async function fetchInternalAPI<Schema extends z.ZodType, AdditionalError
66
80
} )
67
81
68
82
if ( ! response . ok ) {
69
- const additionalError = onApiError ?.( response )
83
+ const additionalError : AdditionalErrors | undefined = onApiError ?.( response )
70
84
if ( additionalError ) {
71
- return additionalError
85
+ return { errorType : additionalError }
72
86
}
73
87
74
88
const responseBody = await getFailedResponseBody ( response )
@@ -77,6 +91,13 @@ export async function fetchInternalAPI<Schema extends z.ZodType, AdditionalError
77
91
return { errorType : 'API_CALL_FAILED' }
78
92
}
79
93
94
+ const isPdfResponse : boolean = response . headers . get ( 'Content-Type' ) ?. includes ( 'application/pdf' ) ?? false
95
+ if ( typeof responseSchema === 'string' && responseSchema === 'ArrayBuffer' ) {
96
+ return ( await response . arrayBuffer ( ) ) as InferredReturnValue
97
+ } else if ( isPdfResponse && responseSchema !== 'ArrayBuffer' ) {
98
+ raise ( `Got PDF but expected response was not ArrayBuffer, for ${ api } ${ path } , whats up?` )
99
+ }
100
+
80
101
const isJsonResponse : boolean = response . headers . get ( 'Content-Type' ) ?. includes ( 'application/json' ) ?? false
81
102
if ( ! isJsonResponse ) {
82
103
raise (
0 commit comments