Last active
September 15, 2023 17:40
-
-
Save ciiqr/27b46e63710d97996979b6b4bb47556f to your computer and use it in GitHub Desktop.
zod type guard for @sendgrid/mail's ResponseError
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTE: sendgrid doesn't properly export its ResponseError class, so this type | |
// (roughly) represents that type (with the type of body fixed) | |
const sendGridResponseErrorSchema = z.instanceof(Error).and(z.object({ | |
code: z.number(), | |
response: z.object({ | |
headers: z.record(z.string(), z.string()), | |
body: z.object({ | |
errors: z.array(z.unknown()) | |
}) | |
}) | |
})); | |
type SendGridResponseError = z.infer<typeof sendGridResponseErrorSchema>; | |
function isSendGridResponseError(val: unknown): val is SendGridResponseError { | |
return sendGridResponseErrorSchema.safeParse(val).success; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment