snap-on-openapi 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -15
- package/dist/OpenApi.js +2 -2
- package/dist/README.md +15 -15
- package/dist/services/Logger/Logger.d.ts +1 -0
- package/dist/services/Logger/Logger.js +3 -0
- package/dist/services/SchemaGenerator/SchemaGenerator.d.ts +1 -1
- package/dist/services/SchemaGenerator/SchemaGenerator.js +2 -3
- package/dist/types/config/Config.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Snap-On OpenAPI
|
|
2
2
|
|
|
3
3
|
Bring a fully-fledged, type-checked API to your app in just 5 minutes.
|
|
4
4
|
|
|
5
|
-
[What is
|
|
5
|
+
[What is Snap-On OpenAPI](#what-is-snap-on-openapi)
|
|
6
6
|
|
|
7
7
|
[Installation](#installation)
|
|
8
8
|
|
|
@@ -21,7 +21,7 @@ Bring a fully-fledged, type-checked API to your app in just 5 minutes.
|
|
|
21
21
|
- TypeScript client generator included
|
|
22
22
|
- Since you have OpenAPI, you can generate clients for any language you want
|
|
23
23
|
|
|
24
|
-
## What is
|
|
24
|
+
## What is Snap-On OpenAPI?
|
|
25
25
|
|
|
26
26
|
OpenAPI is a standard for documenting your REST APIs. It's similar to JSDoc generators but with one major difference: it uses schemas that can be strictly typed and used for code generation.
|
|
27
27
|
|
|
@@ -35,14 +35,14 @@ Zod works so well that I stopped using classes for models and DTOs in my own pro
|
|
|
35
35
|
|
|
36
36
|
You might have the same idea I had some time ago: why not combine Zod and OpenAPI and make our API absolutely type-checked both on the frontend and backend? That would be a blast!
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
Snap-On OpenAPI is a lightweight, non-opinionated framework that allows you to do exactly that. It's highly customizable and easy to use, while providing fully type-checked context. You can forget about those "any" types that pop up here and there in your APIs.
|
|
39
39
|
|
|
40
40
|
The framework doesn't have any predefined middlewares (I don't even use such a concept) or excessive code. It has a few built-in errors and validators which I found helpful, and even those are made with the same utilities that are available to you.
|
|
41
41
|
|
|
42
|
-
Simply put,
|
|
42
|
+
Simply put, Snap-On OpenAPI is the glue that ties together OpenAPI, Zod, and Openapi-TS. And you are in charge of how your API is shaped—that's what sets this framework apart from tools like `GraphQL` and `tRPC`.
|
|
43
43
|
|
|
44
44
|
You can check out some sample code here:
|
|
45
|
-
https://github.com/Freddis/
|
|
45
|
+
https://github.com/Freddis/snap-on-openapi-samples
|
|
46
46
|
|
|
47
47
|
## Disclaimer
|
|
48
48
|
Configuration is a bit clunky due to the huge amount of inferred types. But trust me, when you learn the basics (and there is no advanced level—it's really lightweight) you will be able to configure your API in just 5 minutes.
|
|
@@ -70,13 +70,13 @@ export const upsertWorkouts = openApi.factory.createRoute({
|
|
|
70
70
|
## Installation
|
|
71
71
|
|
|
72
72
|
```shell
|
|
73
|
-
npm install
|
|
73
|
+
npm install snap-on-openapi
|
|
74
74
|
```
|
|
75
75
|
|
|
76
76
|
## Quick Start
|
|
77
|
-
The idea behind
|
|
77
|
+
The idea behind Snap-On OpenAPI is that you don't need to bother with configuration right away. It is designed to be configured as you go. Fire it up, focus on your business logic, then add errors, routes, and contexts as you go.
|
|
78
78
|
|
|
79
|
-
Right now,
|
|
79
|
+
Right now, Snap-On OpenAPI provides quickstart wrappers for Tanstack Start and Express.
|
|
80
80
|
|
|
81
81
|
### Express
|
|
82
82
|
|
|
@@ -183,7 +183,7 @@ As you can see, it's not rocket science to integrate it with any framework.
|
|
|
183
183
|
|
|
184
184
|
## Adding Routes
|
|
185
185
|
|
|
186
|
-
Now let's get deeper with
|
|
186
|
+
Now let's get deeper with Snap-On OpenAPI. Let's add some hot action.
|
|
187
187
|
|
|
188
188
|
We need to create a route and then add it to our OpenAPI instance. I recommend using a separate file for each route and one more file for the route map:
|
|
189
189
|
```typescript
|
|
@@ -229,7 +229,7 @@ export const getCars = openapi.factory.createRoute({
|
|
|
229
229
|
Now let's create a route map:
|
|
230
230
|
```typescript
|
|
231
231
|
// file: src/openapi/routes.ts
|
|
232
|
-
import {OpenApiRouteMap, OpenApiSampleRouteType} from '
|
|
232
|
+
import {OpenApiRouteMap, OpenApiSampleRouteType} from 'snap-on-openapi';
|
|
233
233
|
import {getCars} from './getCars';
|
|
234
234
|
|
|
235
235
|
export const openApiRoutes: OpenApiRouteMap<OpenApiSampleRouteType> = {
|
|
@@ -313,9 +313,9 @@ And you can always write your own wrapper function to make it even less verbose
|
|
|
313
313
|
|
|
314
314
|
## Configuration
|
|
315
315
|
|
|
316
|
-
|
|
316
|
+
Snap-On OpenAPI comes with a default configuration that covers basic errors and provides public route types. You can start with that, but eventually you will grow out of it.
|
|
317
317
|
|
|
318
|
-
There are two ways to configure
|
|
318
|
+
There are two ways to configure Snap-On OpenAPI:
|
|
319
319
|
1. Inferred config (recommended at the beginning)
|
|
320
320
|
2. Implement OpenApiConfig interface
|
|
321
321
|
|
|
@@ -480,7 +480,7 @@ With that said, you can see that you don't have to list all possible kinds of er
|
|
|
480
480
|
|
|
481
481
|
> [!NOTE]
|
|
482
482
|
> Every error that can be thrown corresponds to one or multiple error responses. Whatever happens during API call processing, the consumer will
|
|
483
|
-
> always receive a response. That's why
|
|
483
|
+
> always receive a response. That's why Snap-On OpenAPI requires at least one error response to be defined: it has to have the default error.
|
|
484
484
|
|
|
485
485
|
The `customizeErrors()` call will set you on the path of configuring errors. Similar to `customizeRoutes()`, you won't be able to call `create()` until you have provided everything required for the API to function properly.
|
|
486
486
|
|
|
@@ -538,7 +538,7 @@ export const openApi = OpenApi.builder.customizeErrors(
|
|
|
538
538
|
> [!NOTE]
|
|
539
539
|
> Note that the interface of `defineDefaultError` forces you to use synchronous context. It's no coincidence: errors may happen during your own error handling. This approach guarantees that whatever happens, we always have a suitable response ready.
|
|
540
540
|
|
|
541
|
-
The last thing we need to do is write the error handler itself.
|
|
541
|
+
The last thing we need to do is write the error handler itself. Snap-On OpenAPI can't magically know what error to respond with; the best it can do is respond with the default error response.
|
|
542
542
|
|
|
543
543
|
Surprisingly enough, the last call related to error handling is `customizeGlobalConfig()`, which was already covered above. The reason why it's done this way is to allow you to tweak error handling when you work with `DefaultConfig`. The built-in error types are quite good, and many people may prefer to use them for a while before actually setting up their own error responses.
|
|
544
544
|
|
package/dist/OpenApi.js
CHANGED
|
@@ -31,7 +31,7 @@ export class OpenApi {
|
|
|
31
31
|
servers = [];
|
|
32
32
|
constructor(config) {
|
|
33
33
|
this.config = config;
|
|
34
|
-
this.logger = new Logger('OpenAPI');
|
|
34
|
+
this.logger = config.logger ?? new Logger('OpenAPI');
|
|
35
35
|
if (config.logLevel) {
|
|
36
36
|
Logger.logLevel = config.logLevel;
|
|
37
37
|
}
|
|
@@ -45,7 +45,7 @@ export class OpenApi {
|
|
|
45
45
|
title: config.apiName ?? 'My API',
|
|
46
46
|
version: '3.1.0',
|
|
47
47
|
};
|
|
48
|
-
this.schemaGenerator = new SchemaGenerator(this.logger.
|
|
48
|
+
this.schemaGenerator = new SchemaGenerator(this.logger.derrive('SchemaGenerator'), info, this.config, this.routes, this.servers);
|
|
49
49
|
this.wrappers = {
|
|
50
50
|
tanstackStart: new TanstackStartWrapper(this),
|
|
51
51
|
express: new ExpressWrapper(this),
|
package/dist/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Snap-On OpenAPI
|
|
2
2
|
|
|
3
3
|
Bring a fully-fledged, type-checked API to your app in just 5 minutes.
|
|
4
4
|
|
|
5
|
-
[What is
|
|
5
|
+
[What is Snap-On OpenAPI](#what-is-snap-on-openapi)
|
|
6
6
|
|
|
7
7
|
[Installation](#installation)
|
|
8
8
|
|
|
@@ -21,7 +21,7 @@ Bring a fully-fledged, type-checked API to your app in just 5 minutes.
|
|
|
21
21
|
- TypeScript client generator included
|
|
22
22
|
- Since you have OpenAPI, you can generate clients for any language you want
|
|
23
23
|
|
|
24
|
-
## What is
|
|
24
|
+
## What is Snap-On OpenAPI?
|
|
25
25
|
|
|
26
26
|
OpenAPI is a standard for documenting your REST APIs. It's similar to JSDoc generators but with one major difference: it uses schemas that can be strictly typed and used for code generation.
|
|
27
27
|
|
|
@@ -35,14 +35,14 @@ Zod works so well that I stopped using classes for models and DTOs in my own pro
|
|
|
35
35
|
|
|
36
36
|
You might have the same idea I had some time ago: why not combine Zod and OpenAPI and make our API absolutely type-checked both on the frontend and backend? That would be a blast!
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
Snap-On OpenAPI is a lightweight, non-opinionated framework that allows you to do exactly that. It's highly customizable and easy to use, while providing fully type-checked context. You can forget about those "any" types that pop up here and there in your APIs.
|
|
39
39
|
|
|
40
40
|
The framework doesn't have any predefined middlewares (I don't even use such a concept) or excessive code. It has a few built-in errors and validators which I found helpful, and even those are made with the same utilities that are available to you.
|
|
41
41
|
|
|
42
|
-
Simply put,
|
|
42
|
+
Simply put, Snap-On OpenAPI is the glue that ties together OpenAPI, Zod, and Openapi-TS. And you are in charge of how your API is shaped—that's what sets this framework apart from tools like `GraphQL` and `tRPC`.
|
|
43
43
|
|
|
44
44
|
You can check out some sample code here:
|
|
45
|
-
https://github.com/Freddis/
|
|
45
|
+
https://github.com/Freddis/snap-on-openapi-samples
|
|
46
46
|
|
|
47
47
|
## Disclaimer
|
|
48
48
|
Configuration is a bit clunky due to the huge amount of inferred types. But trust me, when you learn the basics (and there is no advanced level—it's really lightweight) you will be able to configure your API in just 5 minutes.
|
|
@@ -70,13 +70,13 @@ export const upsertWorkouts = openApi.factory.createRoute({
|
|
|
70
70
|
## Installation
|
|
71
71
|
|
|
72
72
|
```shell
|
|
73
|
-
npm install
|
|
73
|
+
npm install snap-on-openapi
|
|
74
74
|
```
|
|
75
75
|
|
|
76
76
|
## Quick Start
|
|
77
|
-
The idea behind
|
|
77
|
+
The idea behind Snap-On OpenAPI is that you don't need to bother with configuration right away. It is designed to be configured as you go. Fire it up, focus on your business logic, then add errors, routes, and contexts as you go.
|
|
78
78
|
|
|
79
|
-
Right now,
|
|
79
|
+
Right now, Snap-On OpenAPI provides quickstart wrappers for Tanstack Start and Express.
|
|
80
80
|
|
|
81
81
|
### Express
|
|
82
82
|
|
|
@@ -183,7 +183,7 @@ As you can see, it's not rocket science to integrate it with any framework.
|
|
|
183
183
|
|
|
184
184
|
## Adding Routes
|
|
185
185
|
|
|
186
|
-
Now let's get deeper with
|
|
186
|
+
Now let's get deeper with Snap-On OpenAPI. Let's add some hot action.
|
|
187
187
|
|
|
188
188
|
We need to create a route and then add it to our OpenAPI instance. I recommend using a separate file for each route and one more file for the route map:
|
|
189
189
|
```typescript
|
|
@@ -229,7 +229,7 @@ export const getCars = openapi.factory.createRoute({
|
|
|
229
229
|
Now let's create a route map:
|
|
230
230
|
```typescript
|
|
231
231
|
// file: src/openapi/routes.ts
|
|
232
|
-
import {OpenApiRouteMap, OpenApiSampleRouteType} from '
|
|
232
|
+
import {OpenApiRouteMap, OpenApiSampleRouteType} from 'snap-on-openapi';
|
|
233
233
|
import {getCars} from './getCars';
|
|
234
234
|
|
|
235
235
|
export const openApiRoutes: OpenApiRouteMap<OpenApiSampleRouteType> = {
|
|
@@ -313,9 +313,9 @@ And you can always write your own wrapper function to make it even less verbose
|
|
|
313
313
|
|
|
314
314
|
## Configuration
|
|
315
315
|
|
|
316
|
-
|
|
316
|
+
Snap-On OpenAPI comes with a default configuration that covers basic errors and provides public route types. You can start with that, but eventually you will grow out of it.
|
|
317
317
|
|
|
318
|
-
There are two ways to configure
|
|
318
|
+
There are two ways to configure Snap-On OpenAPI:
|
|
319
319
|
1. Inferred config (recommended at the beginning)
|
|
320
320
|
2. Implement OpenApiConfig interface
|
|
321
321
|
|
|
@@ -480,7 +480,7 @@ With that said, you can see that you don't have to list all possible kinds of er
|
|
|
480
480
|
|
|
481
481
|
> [!NOTE]
|
|
482
482
|
> Every error that can be thrown corresponds to one or multiple error responses. Whatever happens during API call processing, the consumer will
|
|
483
|
-
> always receive a response. That's why
|
|
483
|
+
> always receive a response. That's why Snap-On OpenAPI requires at least one error response to be defined: it has to have the default error.
|
|
484
484
|
|
|
485
485
|
The `customizeErrors()` call will set you on the path of configuring errors. Similar to `customizeRoutes()`, you won't be able to call `create()` until you have provided everything required for the API to function properly.
|
|
486
486
|
|
|
@@ -538,7 +538,7 @@ export const openApi = OpenApi.builder.customizeErrors(
|
|
|
538
538
|
> [!NOTE]
|
|
539
539
|
> Note that the interface of `defineDefaultError` forces you to use synchronous context. It's no coincidence: errors may happen during your own error handling. This approach guarantees that whatever happens, we always have a suitable response ready.
|
|
540
540
|
|
|
541
|
-
The last thing we need to do is write the error handler itself.
|
|
541
|
+
The last thing we need to do is write the error handler itself. Snap-On OpenAPI can't magically know what error to respond with; the best it can do is respond with the default error response.
|
|
542
542
|
|
|
543
543
|
Surprisingly enough, the last call related to error handling is `customizeGlobalConfig()`, which was already covered above. The reason why it's done this way is to allow you to tweak error handling when you work with `DefaultConfig`. The built-in error types are quite good, and many people may prefer to use them for a while before actually setting up their own error responses.
|
|
544
544
|
|
|
@@ -4,6 +4,7 @@ export declare class Logger {
|
|
|
4
4
|
static logLevel: LogLevel;
|
|
5
5
|
static showTime: boolean;
|
|
6
6
|
constructor(invoker: string, originalInvoker?: string);
|
|
7
|
+
derrive(invoker: string): Logger;
|
|
7
8
|
info(message: string, data?: Record<string, unknown>): void;
|
|
8
9
|
debug(message: string, data?: object): void;
|
|
9
10
|
error(message: string | null, error: unknown, data?: object): void;
|
|
@@ -6,6 +6,9 @@ export class Logger {
|
|
|
6
6
|
constructor(invoker, originalInvoker) {
|
|
7
7
|
this.invoker = (originalInvoker ? `${originalInvoker}:` : '') + invoker;
|
|
8
8
|
}
|
|
9
|
+
derrive(invoker) {
|
|
10
|
+
return new Logger(invoker, this.invoker);
|
|
11
|
+
}
|
|
9
12
|
info(message, data) {
|
|
10
13
|
if (Logger.logLevel === LogLevel.error) {
|
|
11
14
|
return;
|
|
@@ -11,7 +11,7 @@ export declare class SchemaGenerator<TRouteTypes extends string, TErrorCodes ext
|
|
|
11
11
|
protected routes: AnyRoute<TRouteTypes>[];
|
|
12
12
|
protected servers: Server[];
|
|
13
13
|
protected routeSpec: TConfig;
|
|
14
|
-
constructor(
|
|
14
|
+
constructor(logger: Logger, info: Info, spec: TConfig, routes: AnyRoute<TRouteTypes>[], servers: Server[]);
|
|
15
15
|
getYaml(): string;
|
|
16
16
|
saveYaml(path: string): void;
|
|
17
17
|
protected createDocument(): ReturnType<typeof createDocument>;
|
|
@@ -4,15 +4,14 @@ import { stringify } from 'yaml';
|
|
|
4
4
|
import { createDocument } from 'zod-openapi';
|
|
5
5
|
import { z } from 'zod';
|
|
6
6
|
import { Method } from '../../enums/Methods.js';
|
|
7
|
-
import { Logger } from '../Logger/Logger.js';
|
|
8
7
|
export class SchemaGenerator {
|
|
9
8
|
logger;
|
|
10
9
|
info;
|
|
11
10
|
routes;
|
|
12
11
|
servers;
|
|
13
12
|
routeSpec;
|
|
14
|
-
constructor(
|
|
15
|
-
this.logger =
|
|
13
|
+
constructor(logger, info, spec, routes, servers) {
|
|
14
|
+
this.logger = logger;
|
|
16
15
|
this.routes = routes;
|
|
17
16
|
this.routeSpec = spec;
|
|
18
17
|
this.servers = servers;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Logger } from '../../services/Logger/Logger.js';
|
|
1
2
|
import { LogLevel } from '../../services/Logger/types/LogLevel.js';
|
|
2
3
|
import { RoutePath } from '../RoutePath.js';
|
|
3
4
|
import { ErrorConfigMap } from './ErrorConfigMap.js';
|
|
@@ -7,6 +8,7 @@ import { RouteContextMap } from './RouteContextMap.js';
|
|
|
7
8
|
import { RouteExtraPropsMap } from './RouteExtraPropsMap.js';
|
|
8
9
|
import { Server } from './Server.js';
|
|
9
10
|
export type Config<TRouteTypes extends string, TErrorCodes extends string, TErrorConfigMap extends ErrorConfigMap<TErrorCodes>, TRouteParamMap extends RouteExtraPropsMap<TRouteTypes>, TRouteContextMap extends RouteContextMap<TRouteTypes, TRouteParamMap>, TRouteConfigMap extends RouteConfigMap<TRouteTypes, TErrorCodes, TRouteParamMap, TRouteContextMap>> = {
|
|
11
|
+
logger?: Logger;
|
|
10
12
|
basePath: RoutePath;
|
|
11
13
|
routes: TRouteConfigMap;
|
|
12
14
|
errors: TErrorConfigMap;
|