-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: moving types, schemas and fetch logic for features in own file
- Loading branch information
1 parent
a498466
commit 1650aa3
Showing
5 changed files
with
46 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,8 @@ const configSchema = z.object({ | |
NEXT_PUBLIC_OAUTH_CLIENT_ID: z.string().default('425f8b8d-c786-4ff7-b2bf-e52f505fb588'), | ||
NEXT_PUBLIC_OAUTH_SCOPES: z.string().default('openid,offline_access,email,nickname,name,organizations'), | ||
NEXT_PUBLIC_FAKE_TOKEN_ENABLE: z.literal('true').or(z.literal('false')).default('false'), | ||
NEXT_PUBLIC_FAKE_TOKEN: z.object({ sub: z.string().uuid(), name: z.string(), nickname: z.string(), email: z.string().email(), organizations: z.string().array() }).default({ sub: '18159713-5d4e-4ad5-94ad-fbb6bb147984', name: 'Max Mustermann', nickname: 'max.mustermann', email: '[email protected]', organizations: ['3b25c6f5-4705-4074-9fc6-a50c28eba406'] }) | ||
NEXT_PUBLIC_FAKE_TOKEN: z.object({ sub: z.string().uuid(), name: z.string(), nickname: z.string(), email: z.string().email(), organizations: z.string().array() }).default({ sub: '18159713-5d4e-4ad5-94ad-fbb6bb147984', name: 'Max Mustermann', nickname: 'max.mustermann', email: '[email protected]', organizations: ['3b25c6f5-4705-4074-9fc6-a50c28eba406'] }), | ||
NEXT_PUBLIC_FEATURES_FEED_URL: z.string().url().default('https://cdn.helpwave.de/feed.json'), | ||
}).transform(obj => ({ | ||
env: obj.NODE_ENV, | ||
apiUrl: obj.NEXT_PUBLIC_API_URL, | ||
|
@@ -54,6 +55,7 @@ const configSchema = z.object({ | |
}, | ||
fakeTokenEnable: obj.NEXT_PUBLIC_FAKE_TOKEN_ENABLE === 'true', | ||
fakeToken: Buffer.from(JSON.stringify(obj.NEXT_PUBLIC_FAKE_TOKEN)).toString('base64'), | ||
featuresFeedUrl: obj.NEXT_PUBLIC_FEATURES_FEED_URL, | ||
})) | ||
|
||
const getConfig = () => { | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { z } from 'zod' | ||
import { getConfig } from './config'; | ||
|
||
export type Feature = { | ||
title: string, | ||
date: Date, | ||
description: (string | URL)[], | ||
externalResource?: URL | ||
} | ||
|
||
export const featureSchema = z.object({ | ||
title: z.string(), | ||
description: z.string(), | ||
date: z.string(), | ||
image: z.string().url().optional(), | ||
externalResource: z.string().url().optional(), | ||
}).transform<Feature>((obj) => { | ||
let description: (string | URL)[] = [obj.description] | ||
if (obj.image) description = [new URL(obj.image), ...description] | ||
|
||
return { | ||
title: obj.title, | ||
date: new Date(obj.date), | ||
description, | ||
externalResource: obj.externalResource ? new URL(obj.externalResource) : undefined | ||
} | ||
}) | ||
|
||
export const featuresSchema = z.array(featureSchema) | ||
|
||
export const fetchFeatures = (): Promise<Feature[]> => { | ||
const { featuresFeedUrl } = getConfig() | ||
return fetch(featuresFeedUrl) | ||
.then((res) => res.json()) | ||
.then(async (json) => { | ||
await featuresSchema.parseAsync(json) | ||
return json as Feature[] | ||
}) | ||
} |