-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add strict type to handleError * Add test for withValidation * Fix test * Fix withValidation example * Fix wrap * Fix valibot express example * fastify example * Add vite example * vite example with validation * Add invalid response button to vite example * Add rollup-plugin-visualizer to vite example * Fix vite.config.ts
- Loading branch information
Showing
14 changed files
with
1,205 additions
and
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? | ||
|
||
stats.html |
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,12 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>typed-api-spec + Vite</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="/src/main.ts"></script> | ||
</body> | ||
</html> |
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,16 @@ | ||
{ | ||
"name": "vite", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "tsc && vite build", | ||
"preview": "vite preview" | ||
}, | ||
"devDependencies": { | ||
"rollup-plugin-visualizer": "^5.12.0", | ||
"typescript": "~5.6.2", | ||
"vite": "^6.0.1" | ||
} | ||
} |
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,36 @@ | ||
import type FetchT from "@notainc/typed-api-spec/fetch"; | ||
import type { InvalidResponseSpec, Spec } from "./spec.ts"; | ||
export const GITHUB_API_ORIGIN = "https://api.github.com"; | ||
|
||
let fetchGitHub = fetch as FetchT<typeof GITHUB_API_ORIGIN, Spec>; | ||
if (import.meta.env.DEV) { | ||
const { withValidation } = await import("@notainc/typed-api-spec/fetch"); | ||
const { ZodSpec } = await import("./spec.ts"); | ||
const { newZodValidator } = await import("@notainc/typed-api-spec"); | ||
const v = newZodValidator(ZodSpec); | ||
fetchGitHub = withValidation( | ||
fetch, | ||
ZodSpec, | ||
v.req, | ||
v.res, | ||
) as typeof fetchGitHub; | ||
} | ||
|
||
let fetchInvalidResponseGitHub = fetch as FetchT< | ||
typeof GITHUB_API_ORIGIN, | ||
InvalidResponseSpec | ||
>; | ||
if (import.meta.env.DEV) { | ||
const { withValidation } = await import("@notainc/typed-api-spec/fetch"); | ||
const { InvalidResponseZodSpec } = await import("./spec.ts"); | ||
const { newZodValidator } = await import("@notainc/typed-api-spec"); | ||
const v = newZodValidator(InvalidResponseZodSpec); | ||
fetchInvalidResponseGitHub = withValidation( | ||
fetch, | ||
InvalidResponseZodSpec, | ||
v.req, | ||
v.res, | ||
) as typeof fetchInvalidResponseGitHub; | ||
} | ||
|
||
export { fetchGitHub, fetchInvalidResponseGitHub }; |
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 { ZodApiEndpoints } from "@notainc/typed-api-spec"; | ||
import z from "zod"; | ||
import { ToApiEndpoints } from "@notainc/typed-api-spec/zod"; | ||
|
||
// See https://docs.github.com/ja/rest/repos/repos?apiVersion=2022-11-28#get-all-repository-topics | ||
export const ZodSpec = { | ||
"/repos/:owner/:repo/topics": { | ||
get: { | ||
responses: { | ||
200: { body: z.object({ names: z.string().array() }) }, | ||
400: { | ||
body: z.object({ | ||
message: z.string(), | ||
errors: z.string(), | ||
documentation_url: z.string(), | ||
status: z.number(), | ||
}), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} satisfies ZodApiEndpoints; | ||
export type Spec = ToApiEndpoints<typeof ZodSpec>; | ||
|
||
// See https://docs.github.com/ja/rest/repos/repos?apiVersion=2022-11-28#get-all-repository-topics | ||
export const InvalidResponseZodSpec = { | ||
"/repos/:owner/:repo/topics": { | ||
get: { | ||
responses: { | ||
200: { body: z.object({ noexistProps: z.string().array() }) }, | ||
}, | ||
}, | ||
}, | ||
} satisfies ZodApiEndpoints; | ||
export type InvalidResponseSpec = ToApiEndpoints<typeof InvalidResponseZodSpec>; |
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,55 @@ | ||
import "./style.css"; | ||
import { | ||
fetchGitHub, | ||
fetchInvalidResponseGitHub, | ||
GITHUB_API_ORIGIN, | ||
} from "./github/client.ts"; | ||
|
||
document.querySelector<HTMLDivElement>("#app")!.innerHTML = ` | ||
<div> | ||
<h1>typed-api-spec + Vite</h1> | ||
<div class="card"> | ||
<button id="fetch" type="button">Fetch from GitHub</button> | ||
</div> | ||
<div class="card"> | ||
<button id="invalid-fetch" type="button">Invalid fetch from GitHub</button> | ||
</div> | ||
<p id="result"> | ||
Topics of typed-api-spec will be displayed here after clicking the button. | ||
</p> | ||
</div> | ||
`; | ||
|
||
const endpoint = `${GITHUB_API_ORIGIN}/repos/nota/typed-api-spec/topics`; | ||
const result = document.querySelector<HTMLParagraphElement>("#result")!; | ||
|
||
const fetchButton = document.querySelector<HTMLButtonElement>("#fetch")!; | ||
const request = async () => { | ||
result.innerHTML = "Loading..."; | ||
const response = await fetchGitHub(endpoint, {}); | ||
if (!response.ok) { | ||
result.innerHTML = `Error: ${response.status} ${response.statusText}`; | ||
return; | ||
} | ||
const { names } = await response.json(); | ||
result.innerHTML = `Result: ${names.join(", ")}`; | ||
}; | ||
fetchButton.addEventListener("click", () => request()); | ||
|
||
const invalidFetchButton = | ||
document.querySelector<HTMLButtonElement>("#invalid-fetch")!; | ||
const invalidRequest = async () => { | ||
result.innerHTML = "Loading..."; | ||
try { | ||
const response = await fetchInvalidResponseGitHub(endpoint, {}); | ||
if (!response.ok) { | ||
result.innerHTML = `Error: ${response.status} ${response.statusText}`; | ||
return; | ||
} | ||
const { noexistProps } = await response.json(); | ||
result.innerHTML = `Result: ${noexistProps.join(", ")}`; | ||
} catch (e) { | ||
result.innerHTML = `${e}`; | ||
} | ||
}; | ||
invalidFetchButton.addEventListener("click", () => invalidRequest()); |
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,72 @@ | ||
:root { | ||
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; | ||
line-height: 1.5; | ||
font-weight: 400; | ||
|
||
color-scheme: light dark; | ||
color: rgba(255, 255, 255, 0.87); | ||
background-color: #242424; | ||
|
||
font-synthesis: none; | ||
text-rendering: optimizeLegibility; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
|
||
body { | ||
margin: 0; | ||
display: flex; | ||
place-items: center; | ||
min-width: 320px; | ||
min-height: 100vh; | ||
} | ||
|
||
h1 { | ||
font-size: 3.2em; | ||
line-height: 1.1; | ||
} | ||
|
||
#app { | ||
max-width: 1280px; | ||
margin: 0 auto; | ||
padding: 2rem; | ||
text-align: center; | ||
} | ||
|
||
.card { | ||
padding: 2em; | ||
} | ||
|
||
#result { | ||
font-size: 1.3em; | ||
color: rgba(255, 255, 255, 0.87); | ||
} | ||
|
||
button { | ||
border-radius: 8px; | ||
border: 1px solid transparent; | ||
padding: 0.6em 1.2em; | ||
font-size: 1em; | ||
font-weight: 500; | ||
font-family: inherit; | ||
background-color: #1a1a1a; | ||
cursor: pointer; | ||
transition: border-color 0.25s; | ||
} | ||
button:hover { | ||
border-color: #646cff; | ||
} | ||
button:focus, | ||
button:focus-visible { | ||
outline: 4px auto -webkit-focus-ring-color; | ||
} | ||
|
||
@media (prefers-color-scheme: light) { | ||
:root { | ||
color: #213547; | ||
background-color: #ffffff; | ||
} | ||
button { | ||
background-color: #f9f9f9; | ||
} | ||
} |
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 @@ | ||
/// <reference types="vite/client" /> |
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,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"useDefineForClassFields": true, | ||
"module": "ESNext", | ||
"lib": ["ES2020", "DOM", "DOM.Iterable"], | ||
"skipLibCheck": true, | ||
|
||
/* Bundler mode */ | ||
"moduleResolution": "bundler", | ||
"allowImportingTsExtensions": true, | ||
"isolatedModules": true, | ||
"moduleDetection": "force", | ||
"noEmit": true, | ||
|
||
/* Linting */ | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"noUncheckedSideEffectImports": true | ||
}, | ||
"include": ["src"] | ||
} |
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,13 @@ | ||
import { visualizer } from "rollup-plugin-visualizer"; | ||
import { defineConfig } from "vite"; | ||
|
||
export default defineConfig(({ mode }) => { | ||
return { | ||
build: { | ||
target: "esnext", | ||
rollupOptions: { | ||
plugins: [visualizer()], | ||
}, | ||
}, | ||
}; | ||
}); |
Oops, something went wrong.