Skip to content

Commit

Permalink
feat: Replace unknownutil with typia for type checking
Browse files Browse the repository at this point in the history
This commit replaces the unknownutil library with typia for runtime
type checking. It also adds the typia library to the package.json
dependencies and updates the code where unknownutil was previously used.
The changes are mainly in the src/lib/api/rss/index.ts and
src/lib/markdown.server.ts files. Additionally, the unplugin-typia
plugin is added to the vite.config.ts file.
  • Loading branch information
ryoppippi committed Jun 1, 2024
1 parent d759748 commit 06111c0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --plugin prettier-plugin-svelte --plugin prettier-plugin-tailwindcss --check . && eslint .",
"format": "prettier --plugin prettier-plugin-svelte --plugin prettier-plugin-tailwindcss --write .",
"prepare": "git config --local core.hooksPath .githooks || echo 'Can not set git hooks'"
"prepare": "ts-patch install && typia patch && git config --local core.hooksPath .githooks || echo 'Can not set git hooks'"
},
"engines": {
"npm": "use bun please!",
Expand All @@ -35,6 +35,7 @@
"@iconify-json/simple-icons": "^1.1.103",
"@iconify/iconify": "^3.1.1",
"@iconify/svelte": "^4.0.2",
"@ryoppippi/unplugin-typia": "npm:@jsr/ryoppippi__unplugin-typia",
"@secretlint/secretlint-rule-preset-recommend": "^8.2.4",
"@skeletonlabs/skeleton": "^2.10.0",
"@skeletonlabs/tw-plugin": "^0.4.0",
Expand Down Expand Up @@ -73,11 +74,13 @@
"svelte-preprocess-import-assets": "^1.1.0",
"svelte-vertical-timeline": "^1.0.2",
"tailwindcss": "^3.4.3",
"ts-node": "^10.9.2",
"ts-patch": "^3.1.2",
"tslib": "^2.6.2",
"typescript": "^5.4.5",
"typescript-svelte-plugin": "^0.3.38",
"typia": "^6.0.4",
"ufo": "^1.5.3",
"unknownutil": "^3.18.1",
"unplugin-icons": "^0.19.0",
"vite": "^5.2.12"
},
Expand Down
18 changes: 11 additions & 7 deletions src/lib/api/rss/index.js → src/lib/api/rss/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import Parser from 'rss-parser';
import sortOn from 'sort-on';
import { is, ensure } from 'unknownutil';
import typia from 'typia';

import rss from './rss.json';

const parser = new Parser();

export const _isItem = is.ObjectOf({
title: is.String,
link: is.String,
pubDate: is.String
});
type Item = {
title: string;
link: string;
pubDate: string;
};

export const getPosts = async () => {
const feeds = (
await Promise.all(
rss.map(async (url) => {
const feed = await parser.parseURL(url);
return feed.items
.map(({ title, link, pubDate }) => ensure({ title, link, pubDate }, _isItem))
.map(({ title, link, pubDate }) => {
const item = { title, link, pubDate };
typia.assertGuard<Item>(item);
return item;
})
.map((item) => ({ ...item, pubDate: new Date(item.pubDate).toJSON() }));
})
)
Expand Down
32 changes: 14 additions & 18 deletions src/lib/markdown.server.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import markdown from 'markdown-it';
import matter from 'gray-matter';
import { is, assert, type PredicateType } from 'unknownutil';
import { parse } from 'date-fns';
import typia from 'typia';

export const isItem = is.ObjectOf({
slug: is.String,
title: is.String,
pubDate: is.String,
isPublished: is.Boolean
});
type Item = {
slug: string;
title: string;
pubDate: string;
isPublished: boolean;
};

export type Item = PredicateType<typeof isItem>;

export const isMetadata = is.ObjectOf({
title: is.String,
date: is.String,
isPublished: is.Boolean
});

export type Metadata = PredicateType<typeof isMetadata>;
type Metadata = {
title: string;
date: string;
isPublished: boolean;
};

export function parseMarkdown(
filepath: string,
Expand All @@ -44,7 +40,7 @@ export function parseMarkdown<T extends boolean>(

const { data: metadata, content: parsedRawMdContent } = matter(contentRaw);

assert(metadata, isMetadata);
typia.assertGuard<Metadata>(metadata);

const item = {
slug,
Expand All @@ -53,7 +49,7 @@ export function parseMarkdown<T extends boolean>(
pubDate: parse(metadata.date, 'yyyy-MM-dd', new Date()).toJSON()
} satisfies Item;

assert(item, isItem);
typia.assertGuard<Item>(item);

if (options?.parseContent ?? false) {
const md = markdown();
Expand Down
3 changes: 2 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { defineConfig } from 'vite';
import { enhancedImages } from '@sveltejs/enhanced-img';
import Macros from '@unplugin/macros/vite';
import Icons from 'unplugin-icons/vite';
import UnpluginTypia from '@ryoppippi/unplugin-typia/vite';

export default defineConfig({
plugins: [enhancedImages(), Macros(), sveltekit(), Icons({ compiler: 'svelte', autoInstall: true })]
plugins: [UnpluginTypia(), enhancedImages(), Macros(), sveltekit(), Icons({ compiler: 'svelte', autoInstall: true })]
});

0 comments on commit 06111c0

Please sign in to comment.