Skip to content

Commit

Permalink
Republish esm/cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel committed Nov 22, 2020
1 parent 4c87c30 commit fbc4a42
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 26 deletions.
3 changes: 0 additions & 3 deletions .npmignore

This file was deleted.

50 changes: 37 additions & 13 deletions deno_lib/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Zod-endpoints
Zod-router is a contract first strictly typed router. By defining the router as a zod schema, all the requests and responses can be checked and parsed at runtime. Moreover, the TypeScript compiler can check the in- and output types of the routes.
Contract first strictly typed endpoints. By defining the endpoints as a zod schema, all the requests and responses can be checked and parsed at runtime. Moreover, the TypeScript compiler can check the in- and output types of the endpoints.

In this way the problem space is much smaller to begin with. By using zod-router you can rely on the types during development and on validation at runtime. This yields reqests and responses you can trust. The focus can shift more to defining business logic instead of input validation and error handling.
In this way the problem space will become much smaller to begin with. By using zod-endpoints you can rely on the types during development and on validation at runtime. This yields reqests and responses you can trust. The focus can shift more to defining business logic instead of input validation and error handling.

The schema can be used as a contract between consumer and producer. Drivers can be generated from the contract which ensures proper communication between a client and server.


## Simplified model

Zod-router is based on a type representation of a http schema. Below a simplyfied version of the model. The full model can be found here [model](src/model.ts). The model is a union of requests which contains a union of response objects. Both request and response contain a union of body types.
Zod-endpoints is based on a type representation of a http schema. Below a simplyfied version of the model. The full model can be found [here](src/model.ts). The model is a union of requests which contains a union of response objects. Both request and response contain a union of body types.

````ts
type Body = {
Expand All @@ -35,9 +35,9 @@ type Schema = Http | ...Http
````

## Getting started
First step is to define a router by making use of the [zod-router dsl](src/router.ts). Below you can find an example of a simple router. This example contains two endpoints to get and create a project.
First step is to define an endpoint by making use of the [zod-endpoints dsl](src/dsl.ts). Below you can find an example of a simple example. This example contains two endpoints to get and create a project.

### Route
### Define endpoints
````ts
import * as z from "zod-endpoints";

Expand All @@ -46,8 +46,8 @@ const project = z.object({
name: z.string(),
})

const schema = z.router([
z.route({
const schema = z.endpoints([
z.endpoint({
name: "GET_PROJECT",
method: "GET",
path: [z.literal("projects"), z.string().uuid()],
Expand All @@ -61,7 +61,7 @@ const schema = z.router([
}),
],
}),
z.route({
z.endpoint({
name: "CREATE_PROJECT",
method: "POST",
path: [z.literal("projects")],
Expand All @@ -78,8 +78,11 @@ const schema = z.router([
]);
````

### Api
The router can convert into a service with the [Api](src/api.ts) type. This type transforms the schema into an object of the requests. The key of the object is the name of the route the value is a function from the request to a union of the responses. This object is strict typed and exhaustive.
## Api
The endpoints can be convert into a service or a client with the [Api](src/api.ts) type.

### Server
For the type transforms the schema into an object of the requests. The key of the object is the name of the route the value is a function from the request to a union of the responses. This object is strict typed and exhaustive.

```ts
const service = {
Expand All @@ -91,7 +94,7 @@ const service = {
```ts
import * as z from "zod-endpoints";
const api: z.Api<typeof schema> = {
const server: z.Api<typeof schema> = {
"GET_PROJECT": ({path}) => findProjectById(path[1]).then(project => ({
status: 200,
body:{
Expand All @@ -106,9 +109,30 @@ const api: z.Api<typeof schema> = {
```

### Client
The client implementation

## Open api 3
Zod router is fully compatible with [open api specification](https://www.openapis.org/). The schema can be transformed into open api json. For example with Swagger this can be presented as a documentation website.
```ts
const http = (req: z.ApiRequest) => {
fetch(req.path.join('/'), {
method: req.method
})
}
````
```ts
import * as z from "zod-endpoints";

const client: z.Api<typeof schema> = {
"GET_PROJECT": (req) => http(req),
"CREATE_PROJECT": (req) => http(req)
};
```

## Documentation
Zod endpoints is fully compatible with [open api specification](https://www.openapis.org/). The schema can be transformed into open api json. For example with Swagger this can be presented as a documentation website.

```ts
const docs = z.openApi(schema)
````
![GitHub Logo](images/pets_swagger.png)

File renamed without changes.
8 changes: 4 additions & 4 deletions deno_lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as z from "./deps.ts";
import { HttpSchema } from "./model.ts";
import {HttpSchema} from "./model.ts";

export type ApiRouteNames<T extends HttpSchema> = z.output<T> extends {
export type ApiEndpointNames<T extends HttpSchema> = z.output<T> extends {
name: string;
}
? z.output<T>["name"]
Expand All @@ -19,9 +19,9 @@ export type ApiRouteFunction<T extends HttpSchema, Key> = (
) => Promise<ApiResponse<T, Key>>;

export type Api<T extends HttpSchema> = {
[Key in ApiRouteNames<T>]: ApiRouteFunction<T, Key>;
[Key in ApiEndpointNames<T>]: ApiRouteFunction<T, Key>;
};
export type ApiFragment<
T extends HttpSchema,
Key extends ApiRouteNames<T>
Key extends ApiEndpointNames<T>
> = Pick<Api<T>, Key>;
2 changes: 1 addition & 1 deletion deno_lib/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./index.ts";
export * from "./cjs/index.ts";
2 changes: 1 addition & 1 deletion package-lock.json

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

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"name": "zod-endpoints",
"version": "0.0.2",
"version": "0.0.3",
"description": "Typescript contract first strictly typed endpoints",
"main": "./lib/index.js",
"main": "./lib/cjs/index.js",
"types": "./lib/cjs/index.d.ts",
"module": "./lib/esm/index.js",
"files": [
"lib"
],
Expand All @@ -28,7 +30,7 @@
],
"scripts": {
"clean": "rm -rf lib/*",
"build": "yarn run clean && tsc --p tsconfig.json",
"build": "yarn run clean && tsc --p tsconfig.cjs.json && tsc --p tsconfig.esm.json",
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
"lint": "tslint -p tsconfig.json",
"test": "jest",
Expand Down
6 changes: 6 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./lib/cjs"
}
}
7 changes: 7 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "esnext",
"outDir": "./lib/esm"
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
"esnext",
"dom"
],
"outDir": "./lib",
"target": "es5",
"removeComments": true,
"module": "commonjs",
"declaration": true,
"outDir": "./lib",
"esModuleInterop": true,
"sourceMap": true,
"moduleResolution": "node",
Expand Down

0 comments on commit fbc4a42

Please sign in to comment.