Skip to content

Add sketch of typed api #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions modules/api-definition/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
v8-compile-cache-*
dist
*.tsbuildinfo
.*-audit.json
55 changes: 55 additions & 0 deletions modules/api-definition/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "@myrepo/api-definition",
"version": "0.0.0",
"scripts": {
"test": "vitest run --coverage",
"compile": "tsc --noEmit",
"test:watch": "vitest",
"changeset": "echo 'Please run `pnpm changeset` from the monorepo root'",
"prepublishOnly": "pnpm run build",
"build": "vite build"
},
"type": "module",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"@myrepo": "./src/index.ts",
"types": "./dist/api-definition/src/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/api-definition/src/index.d.ts",
"default": "./dist/index.cjs"
}
}
},
"dependencies": {
"@myrepo/sum": "workspace:^",
"@zodios/core": "^10.9.6",
"zod": "^3.24.3"
},
"files": [
"README.md",
"dist",
"src"
],
"repository": {
"type": "git",
"url": "https://github.com/cefn/nextjs-typescript-monorepo.git",
"directory": "modules/api-definition"
},
"homepage": "https://github.com/cefn/nextjs-typescript-monorepo/tree/main/modules/api-definition/README.md",
"bugs": {
"url": "https://github.com/cefn/nextjs-typescript-monorepo/issues"
},
"devDependencies": {
"@rollup/plugin-typescript": "^11.1.6",
"@vitest/coverage-v8": "^2.1.8",
"typescript": "^5.8.3",
"vite": "^6.0.6",
"vite-plugin-dts": "^4.4.0",
"vite-plugin-externalize-deps": "^0.8.0",
"vitest": "^2.1.8"
}
}
60 changes: 60 additions & 0 deletions modules/api-definition/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { makeApi } from "@zodios/core";
import { z } from "zod";

const DEFINITION_SCHEMA = z.object({
definition: z.string(),
synonyms: z.array(z.string()),
antonyms: z.array(z.string()),
});

const WORD_SCHEMA = z.object({
word: z.string(),
phonetic: z.string(),
meanings: z.object({
partOfSpeech: z.string(),
definitions: z.array(DEFINITION_SCHEMA),
}),
sourceUrls: z.array(z.string().url()),
});

export const DICTIONARY_DOWNSTREAM_RESPONSE_SCHEMA = z.array(WORD_SCHEMA);

export type DictionaryDownstreamResponse = z.infer<
typeof DICTIONARY_DOWNSTREAM_RESPONSE_SCHEMA
>;

const DICTIONARY_BFF_SUCCESS_SCHEMA = z.object({
httpStatus: z.literal(200),
data: DICTIONARY_DOWNSTREAM_RESPONSE_SCHEMA,
});

const DICTIONARY_BFF_FAILURE_SCHEMA = z.object({
httpStatus: z.union([z.literal(400), z.literal(500), z.literal(503)]),
message: z.string(),
});

const DICTIONARY_BFF_RESPONSE_SCHEMA = z.union([
DICTIONARY_BFF_SUCCESS_SCHEMA,
DICTIONARY_BFF_FAILURE_SCHEMA,
]);

export type DictionaryBffResponse = z.infer<
typeof DICTIONARY_BFF_RESPONSE_SCHEMA
>;

export const API = makeApi([
{
method: "get",
path: "/dictionary/:query",
description: "Lookup a word",
parameters: [
{
name: "query",
type: "Path",
schema: z.string(),
description: "The word to look up",
},
],
response: DICTIONARY_BFF_RESPONSE_SCHEMA,
},
]);
7 changes: 7 additions & 0 deletions modules/api-definition/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Zodios } from "@zodios/core";

import { API } from "./api.ts";

export function createClient(endpoint: string) {
return new Zodios(endpoint, API);
}
2 changes: 2 additions & 0 deletions modules/api-definition/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./api.ts";
export * from "./client.ts";
8 changes: 8 additions & 0 deletions modules/api-definition/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*"],
"compilerOptions": {
"outDir": "./dist",
"declaration": true
}
}
39 changes: 39 additions & 0 deletions modules/api-definition/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import typescript from "@rollup/plugin-typescript";
import path from "path";
import dts from "vite-plugin-dts";
import { externalizeDeps } from "vite-plugin-externalize-deps";
import { defineConfig } from "vitest/config";

import { coverage } from "./vite.coverage.ts";

export default defineConfig({
resolve: {
conditions: ["@myrepo"],
},
build: {
rollupOptions: {
plugins: [
typescript({
project: "./tsconfig.json",
sourceMap: true,
declaration: true,
outDir: "dist",
module: "nodeNext",
}),
],
},
sourcemap: "inline",
minify: false,
lib: {
entry: path.resolve(__dirname, "src/index.ts"),
fileName: "index",
formats: ["es", "cjs"],
},
},
plugins: [dts(), externalizeDeps()],
test: {
reporters: ["verbose"],
disableConsoleIntercept: true,
coverage,
},
});
72 changes: 72 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions servers/express-with-dependencies/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@
"private": true,
"type": "module",
"dependencies": {
"@myrepo/api-definition": "workspace:^",
"@myrepo/multiply": "workspace:^",
"@myrepo/sum": "workspace:^",
"@zodios/core": "^10.9.6",
"@zodios/express": "^10.6.1",
"express": "^4.21.2",
"tsx": "^4.19.2"
},
"devDependencies": {
"@types/express": "^5.0.0",
"wait-on": "^8.0.2",
"typescript": "^5.8.3",
"vitest": "^2.1.8"
"vitest": "^2.1.8",
"wait-on": "^8.0.2"
},
"repository": {
"type": "git",
Expand All @@ -30,4 +33,4 @@
"bugs": {
"url": "https://github.com/cefn/nextjs-typescript-monorepo/issues"
}
}
}
Empty file.