Skip to content

Commit

Permalink
fix: content type parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra committed Nov 7, 2024
1 parent 86bebf1 commit 74ff4a8
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/oauth2/src/metadata/fetch-well-known-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function fetchWellKnownMetadata<Schema extends BaseSchema>(

if (!response.ok) {
throw new Oauth2InvalidFetchResponseError(
`Fetching well known metadata from '${wellKnownMetadataUrl}' did resulted in an unsuccessfull response with status '${response.status}'.`,
`Fetching well known metadata from '${wellKnownMetadataUrl}' resulted in an unsuccessfull response with status '${response.status}'.`,
await response.clone().text(),
response
)
Expand Down
2 changes: 1 addition & 1 deletion packages/oid4vci/src/credential-offer/credential-offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function resolveCredentialOffer(
const { response, result } = await fetchWithValibot(vCredentialOfferObject, parsedQueryParams.credential_offer_uri)
if (!response.ok || !result) {
throw new Oauth2InvalidFetchResponseError(
`Fetching well known metadata from '${parsedQueryParams.credential_offer_uri}' did resulted in an unsuccesfull response with status '${response.status}'`,
`Fetching credential offer from '${parsedQueryParams.credential_offer_uri}' resulted in an unsuccesfull response with status '${response.status}'`,
await response.clone().text(),
response
)
Expand Down
9 changes: 4 additions & 5 deletions packages/oid4vci/src/notification/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
type ResourceRequestResponseOk,
resourceRequest,
} from '@animo-id/oauth2'
import { ContentType, parseWithErrorHandling } from '@animo-id/oauth2-utils'
import { ContentType, isResponseContentType, parseWithErrorHandling } from '@animo-id/oauth2-utils'
import type { IssuerMetadataResult } from '../metadata/fetch-issuer-metadata'
import {
type NotificationEvent,
Expand Down Expand Up @@ -106,10 +106,9 @@ export async function sendNotifcation(
})

if (!resourceResponse.ok) {
const notificationErrorResponseResult =
resourceResponse.response.headers.get('Content-Type') === ContentType.Json
? v.safeParse(vNotificationErrorResponse, await resourceResponse.response.clone().json())
: undefined
const notificationErrorResponseResult = isResponseContentType(ContentType.Json, resourceResponse.response)
? v.safeParse(vNotificationErrorResponse, await resourceResponse.response.clone().json())
: undefined

return {
...resourceResponse,
Expand Down
12 changes: 12 additions & 0 deletions packages/utils/src/content-type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import type { FetchResponse } from './globals'

export enum ContentType {
XWwwFormUrlencoded = 'application/x-www-form-urlencoded',
Json = 'application/json',
}

export function isContentType(contentType: ContentType, value: string) {
return value.toLowerCase().trim().split(';')[0] === contentType
}

export function isResponseContentType(contentType: ContentType, response: FetchResponse) {
const header = response.headers.get('Content-Type')
if (!header) return false
return isContentType(contentType, header)
}
2 changes: 1 addition & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ export {
type WwwAuthenticateHeaderChallenge,
encodeWwwAuthenticateHeader,
} from './www-authenticate'
export { ContentType } from './content-type'
export { ContentType, isContentType, isResponseContentType } from './content-type'
export { setGlobalConfig, type Oid4vcTsConfig, getGlobalConfig } from './config'
4 changes: 2 additions & 2 deletions packages/utils/src/valibot-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as v from 'valibot'
import { ContentType } from './content-type'
import { ContentType, isResponseContentType } from './content-type'
import type { Fetch } from './globals'

// biome-ignore lint/suspicious/noExplicitAny: any type needed for generic
Expand Down Expand Up @@ -46,7 +46,7 @@ export function createValibotFetcher(
return {
response,
result:
response.ok && response.headers.get('Content-Type') === ContentType.Json
response.ok && isResponseContentType(ContentType.Json, response)
? v.safeParse(schema, await response.json())
: undefined,
}
Expand Down

0 comments on commit 74ff4a8

Please sign in to comment.