Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept text (not just json) in typed API #30

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion client/src/api/useApiMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ export function useApiMutation<
headers,
body,
})
const result = await response.json().catch(() => {})
const result = response.headers.get("Content-Type")?.includes("application/json")
? await response.json().catch(() => {})
: await response.text().catch(() => {})
if (response.status < 200 || response.status >= 300) throw result
return result as Prettify<DefResponse<Def>>
},
Expand Down
4 changes: 3 additions & 1 deletion client/src/api/useApiQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export function useApiQuery<Def extends ClientDefinition, T = Prettify<DefRespon
// Headers are placed in the headers
const headers = makeHeaders(data?.Headers as Record<string, unknown>)
const response = await fetch(withBody, { method, headers })
const result = await response.json().catch(() => {})
const result = response.headers.get("Content-Type")?.includes("application/json")
? await response.json().catch(() => {})
: await response.text().catch(() => {})
if (response.status < 200 || response.status >= 300) throw result
return result as Prettify<DefResponse<Def>>
},
Expand Down
4 changes: 2 additions & 2 deletions server/src/api/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type BaseSchema = {
params?: JSONSchema7 & { type: "object" }
headers?: JSONSchema7 & { type: "object" }
response?: {
[key in HttpKeys]?: JSONSchema7 & { type: "object" }
[key in HttpKeys]?: JSONSchema7 & { type: "object" | "string" }
}
}

Expand All @@ -27,7 +27,7 @@ export type ClientDefinition = {
Params?: Record<string, any>
Headers?: Record<string, any>
Reply?: {
[Code in HttpKeys]?: Record<string, any>
[Code in HttpKeys]?: Record<string, any> | string
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions server/src/api/routes/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ const schema = {
additionalProperties: false,
},
201: {
type: "object",
properties: {
michel: { type: "number" },
},
required: ["michel"],
type: "string",
enum: ["red", "amber", "green"],
},
404: {
type: "object",
Expand Down Expand Up @@ -54,7 +51,8 @@ export const handler = procedure(schema, definition, {
request.log.info("hello world", request.query.id, request.headers["x-id"])
if (request.query.id === "42") {
void reply.code(200).send({ hello: "world" })
// void reply.status(200).send({ hello: "world" })
} else if (request.headers["x-id"] === "123") {
void reply.code(201).send("amber")
} else {
void reply.code(404).send({ error: "no" })
}
Expand Down