generated from matthieu-locussol/starter-turborepo-websocket-zod
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: changelogs are derived from github releases
- Loading branch information
1 parent
f3552d1
commit 5c24218
Showing
11 changed files
with
140 additions
and
109 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 |
---|---|---|
@@ -1,22 +1,32 @@ | ||
import { RequestHandler } from 'express'; | ||
import { ChangelogSchema } from 'shared'; | ||
import { prisma } from '../utils/prisma'; | ||
import { ChangelogSchema, fetchGitHubReleases } from 'shared'; | ||
|
||
const CHANGELOG_DEFAULT_MESSAGE = 'See the assets to download this version and install it.'; | ||
|
||
export const changelogRouter: RequestHandler = async (_, res) => { | ||
const changelogs = await prisma.changelog.findMany({ | ||
orderBy: { | ||
date: 'desc', | ||
}, | ||
take: 10, | ||
}); | ||
try { | ||
const releases = await fetchGitHubReleases(); | ||
|
||
const payload: ChangelogSchema = { | ||
changelogs: releases | ||
.map((release) => ({ | ||
...release, | ||
body: release.body.replace(new RegExp(CHANGELOG_DEFAULT_MESSAGE, 'g'), ''), | ||
})) | ||
.filter(({ body }) => body !== '') | ||
.map(({ tag_name, published_at, body }) => ({ | ||
id: tag_name, | ||
date: published_at, | ||
text: body, | ||
})), | ||
}; | ||
|
||
const result: ChangelogSchema = { | ||
changelogs: changelogs.map((changelog) => ({ | ||
id: changelog.id, | ||
date: changelog.date.toISOString(), | ||
text: changelog.text, | ||
})), | ||
}; | ||
res.send(payload); | ||
} catch (e) { | ||
const payload: ChangelogSchema = { | ||
changelogs: [], | ||
}; | ||
|
||
res.send(result); | ||
res.send(payload); | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { z } from 'zod'; | ||
|
||
export const GITHUB_RELEASES_ENDPOINT = | ||
'https://api.github.com/repos/matthieu-locussol/taktix-app/releases'; | ||
|
||
export const GITHUB_LATEST_RELEASE_ENDPOINT = | ||
'https://api.github.com/repos/matthieu-locussol/taktix-app/releases/latest'; | ||
|
||
export const zGitHubReleaseAsset = z.object({ | ||
name: z.string(), | ||
browser_download_url: z.string(), | ||
}); | ||
|
||
export type GitHubReleaseAsset = z.infer<typeof zGitHubReleaseAsset>; | ||
|
||
export const zGitHubRelease = z.object({ | ||
tag_name: z.string(), | ||
published_at: z.string(), | ||
assets: z.array(zGitHubReleaseAsset), | ||
body: z.string(), | ||
}); | ||
|
||
export type GitHubRelease = z.infer<typeof zGitHubRelease>; | ||
|
||
export const fetchGitHubReleases = async () => { | ||
const response = await fetch(GITHUB_RELEASES_ENDPOINT); | ||
const json = await response.json(); | ||
|
||
return z.array(zGitHubRelease).parse(json); | ||
}; | ||
|
||
export const fetchLatestGitHubRelease = async () => { | ||
const response = await fetch(GITHUB_LATEST_RELEASE_ENDPOINT); | ||
const json = await response.json(); | ||
|
||
return zGitHubRelease.parse(json); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { z } from 'zod'; | ||
import { ZodMgt } from './zodMgt'; | ||
|
||
describe('ZodMgt', () => { | ||
describe('isValidZodLiteralUnion', () => { | ||
it('should return true if the literals array has at least 2 elements', () => { | ||
const literals = [z.literal('a'), z.literal('b')]; | ||
expect(ZodMgt.isValidZodLiteralUnion(literals)).toBe(true); | ||
}); | ||
|
||
it('should return false if the literals array has less than 2 elements', () => { | ||
const literals = [z.literal('a')]; | ||
expect(ZodMgt.isValidZodLiteralUnion(literals)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('constructZodLiteralUnionType', () => { | ||
it('should return a ZodUnion schema of the literals passed', () => { | ||
const literals = [z.literal('a'), z.literal('b')]; | ||
const union = ZodMgt.constructZodLiteralUnionType(literals); | ||
|
||
expect(JSON.stringify(union)).toEqual( | ||
JSON.stringify(z.union([z.literal('a'), z.literal('b')])), | ||
); | ||
}); | ||
|
||
it('should throw an error if the literals array has less than 2 elements', () => { | ||
const literals = [z.literal('a')]; | ||
expect(() => ZodMgt.constructZodLiteralUnionType(literals)).toThrowError( | ||
'Literals passed do not meet the criteria for constructing a union schema, the minimum length is 2', | ||
); | ||
}); | ||
}); | ||
}); |
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,17 @@ | ||
import { z } from 'zod'; | ||
|
||
export namespace ZodMgt { | ||
export const isValidZodLiteralUnion = <T extends z.ZodLiteral<unknown>>( | ||
literals: T[], | ||
): literals is [T, T, ...T[]] => literals.length >= 2; | ||
|
||
export const constructZodLiteralUnionType = <T extends z.ZodLiteral<unknown>>(literals: T[]) => { | ||
if (!isValidZodLiteralUnion(literals)) { | ||
throw new Error( | ||
'Literals passed do not meet the criteria for constructing a union schema, the minimum length is 2', | ||
); | ||
} | ||
|
||
return z.union(literals); | ||
}; | ||
} |