@chubbyts/chubbyts-api
Version:
[](https://github.com/chubbyts/chubbyts-api/actions?query=workflow%3ACI) [ • 11.1 kB
Markdown
# chubbyts-api
[](https://github.com/chubbyts/chubbyts-api/actions?query=workflow%3ACI)
[](https://coveralls.io/github/chubbyts/chubbyts-api?branch=master)
[](https://dashboard.stryker-mutator.io/reports/github.com/chubbyts/chubbyts-api/master)
[](https://www.npmjs.com/package/@chubbyts/chubbyts-api)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-api)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-api)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-api)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-api)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-api)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-api)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-api)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-api)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-api)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-api)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-api)
## Description
## Requirements
* node: 18
* [@chubbyts/chubbyts-decode-encode][2]: ^2.0.0
* [@chubbyts/chubbyts-http-error][3]: ^3.0.0
* [@chubbyts/chubbyts-http-types][4]: ^3.0.0
* [@chubbyts/chubbyts-log-types][5]: ^3.0.0
* [@chubbyts/chubbyts-negotiation][6]: ^4.0.0
* [@chubbyts/chubbyts-throwable-to-error][7]: ^2.0.0
* [qs][8]: ^6.14.0
* [uuid][9]: ^11.1.0
* [zod][10]: ^3.25.64
## Installation
Through [NPM](https://www.npmjs.com) as [@chubbyts/chubbyts-api][1].
```ts
npm i @chubbyts/chubbyts-api@^5.0.1
```
## Usage
### Handler
#### createListHandler
```ts
import { createListHandler } from '@chubbyts/chubbyts-api/dist/handler/list';
import { createEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder';
import { createJsonTypeEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder';
import { z } from 'zod';
import { ResolveModelList } from '@chubbyts/chubbyts-api/dist/repository';
import { List, Model } from '@chubbyts/chubbyts-api/dist/model';
import {
createResponseFactory,
createServerRequestFactory,
} from '@chubbyts/chubbyts-http/dist/message-factory'; // any implementation can be used
const querySchema = z.object({ name: z.string() }).strict();
const resolveModelList: ResolveModelList<MyModel> = (list: List<Model<MyModel>>): Promise<List<Model<MyModel>>> => {};
const responseFactory = createResponseFactory();
const modelListResponseSchema = z.object({ id: z.string(), createdAt: z.date(), name: z.string() }).strict();
const encoder = createEncoder([createJsonTypeEncoder()]);
const serverRequestFactory = createServerRequestFactory();
const listHandler = createListHandler<MyModel>(
querySchema,
resolveModelList,
responseFactory,
modelListResponseSchema,
encoder
);
(async () => {
const request = serverRequestFactory('GET', 'http://localhost:8080/api/pets');
const response = await listHandler(request);
})();
```
#### createCreateHandler
```ts
import { createCreateHandler } from '@chubbyts/chubbyts-api/dist/handler/create';
import { createDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder';
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/json-type-decoder';
import { createEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder';
import { createJsonTypeEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder';
import { z } from 'zod';
import { PersistModel } from '@chubbyts/chubbyts-api/dist/repository';
import { Model } from '@chubbyts/chubbyts-api/dist/model';
import {
createResponseFactory,
createServerRequestFactory,
} from '@chubbyts/chubbyts-http/dist/message-factory'; // any implementation can be used
const decoder = createDecoder([createJsonTypeDecoder()]);
const modelRequestSchema = z.object({ name: z.string() }).strict();
const persistModel: PersistModel<MyModel> = (model: Model<MyModel>): Promise<Model<MyModel>> => {};
const responseFactory = createResponseFactory();
const modelResponseSchema = z.object({ id: z.string(), createdAt: z.date(), name: z.string() }).strict();
const encoder = createEncoder([createJsonTypeEncoder()]);
const serverRequestFactory = createServerRequestFactory();
const createHandler = createCreateHandler<Model>(
decoder,
modelRequestSchema,
persistModel,
responseFactory,
modelResponseSchema,
encoder
);
(async () => {
const request = serverRequestFactory('POST', 'http://localhost:8080/api/pets');
const response = await createHandler(request);
})();
```
#### createReadHandler
```ts
import { createReadHandler } from '@chubbyts/chubbyts-api/dist/handler/read';
import { createEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder';
import { createJsonTypeEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder';
import { z } from 'zod';
import { FindModelById } from '@chubbyts/chubbyts-api/dist/repository';
import { Model } from '@chubbyts/chubbyts-api/dist/model';
import {
createResponseFactory,
createServerRequestFactory,
} from '@chubbyts/chubbyts-http/dist/message-factory'; // any implementation can be used
const findModelById: FindModelById<MyModel> = async (id: string): Promise<Model<MyModel>|undefined> => {};
const responseFactory = createResponseFactory();
const modelResponseSchema = z.object({ id: z.string(), createdAt: z.date(), name: z.string() }).strict();
const encoder = createEncoder([createJsonTypeEncoder()]);
const serverRequestFactory = createServerRequestFactory();
const readHandler = createReadHandler<Model>(
findModelById,
responseFactory,
modelResponseSchema,
encoder
);
(async () => {
const request = serverRequestFactory('GET', 'http://localhost:8080/api/pets/8ba9661b-ba7f-436b-bd25-c0606f911f7d');
const response = await readHandler(request);
})();
```
#### createUpdateHandler
```ts
import { createUpdateHandler } from '@chubbyts/chubbyts-api/dist/handler/update';
import { createDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder';
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/json-type-decoder';
import { createEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder';
import { createJsonTypeEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder';
import { z } from 'zod';
import { FindModelById, PersistModel } from '@chubbyts/chubbyts-api/dist/repository';
import { Model } from '@chubbyts/chubbyts-api/dist/model';
import {
createResponseFactory,
createServerRequestFactory,
} from '@chubbyts/chubbyts-http/dist/message-factory'; // any implementation can be used
const findModelById: FindModelById<MyModel> = async (id: string): Promise<Model<MyModel>|undefined> => {};
const decoder = createDecoder([createJsonTypeDecoder()]);
const modelRequestSchema = z.object({ name: z.string() }).strict();
const persistModel: PersistModel<MyModel> = (model: Model<MyModel>): Promise<Model<MyModel>> => {};
const responseFactory = createResponseFactory();
const modelResponseSchema = z.object({ id: z.string(), createdAt: z.date(), name: z.string() }).strict();
const encoder = createEncoder([createJsonTypeEncoder()]);
const serverRequestFactory = createServerRequestFactory();
const updateHandler = createUpdateHandler<Model>(
findModelById,
decoder,
modelRequestSchema,
persistModel,
responseFactory,
modelResponseSchema,
encoder
);
(async () => {
const request = serverRequestFactory('PUT', 'http://localhost:8080/api/pets/8ba9661b-ba7f-436b-bd25-c0606f911f7d');
const response = await updateHandler(request);
})();
```
#### createDeleteHandler
```ts
import { createDeleteHandler } from '@chubbyts/chubbyts-api/dist/handler/delete';
import { FindModelById, RemoveModel } from '@chubbyts/chubbyts-api/dist/repository';
import { Model } from '@chubbyts/chubbyts-api/dist/model';
import {
createResponseFactory,
createServerRequestFactory,
} from '@chubbyts/chubbyts-http/dist/message-factory'; // any implementation can be used
const findModelById: FindModelById<MyModel> = async (id: string): Promise<Model<MyModel>|undefined> => {};
const removeModel: RemoveModel<MyModel> = (model: Model<MyModel>): Promise<void> => {};
const responseFactory = createResponseFactory();
const serverRequestFactory = createServerRequestFactory();
const deleteHandler = createDeleteHandler<Model>(
findModelById,
removeModel,
responseFactory,
);
(async () => {
const request = serverRequestFactory('DELETE', 'http://localhost:8080/api/pets/8ba9661b-ba7f-436b-bd25-c0606f911f7d');
const response = await deleteHandler(request);
})();
```
### Middleware
#### createAcceptLanguageNegotiationMiddleware
#### createAcceptNegotiationMiddleware
#### createContentTypeNegotiationMiddleware
#### createErrorMiddleware
## Copyright
2025 Dominik Zogg
[1]: https://www.npmjs.com/package/@chubbyts/chubbyts-api
[2]: https://www.npmjs.com/package/@chubbyts/chubbyts-decode-encode
[3]: https://www.npmjs.com/package/@chubbyts/chubbyts-http-error
[4]: https://www.npmjs.com/package/@chubbyts/chubbyts-http-types
[5]: https://www.npmjs.com/package/@chubbyts/chubbyts-log-types
[6]: https://www.npmjs.com/package/@chubbyts/chubbyts-negotiation
[7]: https://www.npmjs.com/package/@chubbyts/chubbyts-throwable-to-error
[8]: https://www.npmjs.com/package/qs
[9]: https://www.npmjs.com/package/uuid
[10]: https://www.npmjs.com/package/zod