@chubbyts/chubbyts-api
v4.1.1
Published
[![CI](https://github.com/chubbyts/chubbyts-api/workflows/CI/badge.svg?branch=master)](https://github.com/chubbyts/chubbyts-api/actions?query=workflow%3ACI) [![Coverage Status](https://coveralls.io/repos/github/chubbyts/chubbyts-api/badge.svg?branch=maste
Downloads
550
Readme
chubbyts-api
Description
Requirements
- node: 16
- @chubbyts/chubbyts-decode-encode: ^1.3.1
- @chubbyts/chubbyts-http-error: ^2.3.1
- @chubbyts/chubbyts-http-types: ^1.2.3
- @chubbyts/chubbyts-log-types: ^1.3.2
- @chubbyts/chubbyts-negotiation: ^3.2.2
- @chubbyts/chubbyts-throwable-to-error: ^1.2.1
- qs: ^6.12.1
- uuid: ^10.0.0
- zod: ^3.23.8
Installation
Through NPM as @chubbyts/chubbyts-api.
npm i @chubbyts/chubbyts-api@^4.1.1
Usage
Handler
createListHandler
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 { ResolveList } 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 inputSchema = z.object({ name: z.string() }).strict();
const resolveList: ResolveList<MyModel> = (list: List<Model<MyModel>>): Promise<List<Model<MyModel>>> => {};
const responseFactory = createResponseFactory();
const outputSchema = z.object({ id: z.string(), createdAt: z.date(), name: z.string() }).strict();
const encoder = createEncoder([createJsonTypeEncoder()]);
const serverRequestFactory = createServerRequestFactory();
const listHandler = createListHandler<MyModel>(
inputSchema,
resolveList,
responseFactory,
outputSchema,
encoder
);
(async () => {
const request = serverRequestFactory('GET', 'http://localhost:8080/api/pets');
const response = await listHandler(request);
})();
createCreateHandler
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 { Persist } 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 inputSchema = z.object({ name: z.string() }).strict();
const persist: Persist<MyModel> = (model: Model<MyModel>): Promise<Model<MyModel>> => {};
const responseFactory = createResponseFactory();
const outputSchema = z.object({ id: z.string(), createdAt: z.date(), name: z.string() }).strict();
const encoder = createEncoder([createJsonTypeEncoder()]);
const serverRequestFactory = createServerRequestFactory();
const createHandler = createCreateHandler<Model>(
decoder,
inputSchema,
persist,
responseFactory,
outputSchema,
encoder
);
(async () => {
const request = serverRequestFactory('POST', 'http://localhost:8080/api/pets');
const response = await createHandler(request);
})();
createReadHandler
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 { FindOneById } 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 findOneById: FindOneById<MyModel> = async (id: string): Promise<Model<MyModel>|undefined> => {};
const responseFactory = createResponseFactory();
const outputSchema = z.object({ id: z.string(), createdAt: z.date(), name: z.string() }).strict();
const encoder = createEncoder([createJsonTypeEncoder()]);
const serverRequestFactory = createServerRequestFactory();
const readHandler = createReadHandler<Model>(
findOneById,
responseFactory,
outputSchema,
encoder
);
(async () => {
const request = serverRequestFactory('GET', 'http://localhost:8080/api/pets/8ba9661b-ba7f-436b-bd25-c0606f911f7d');
const response = await readHandler(request);
})();
createUpdateHandler
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 { FindOneById, Persist } 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 findOneById: FindOneById<MyModel> = async (id: string): Promise<Model<MyModel>|undefined> => {};
const decoder = createDecoder([createJsonTypeDecoder()]);
const inputSchema = z.object({ name: z.string() }).strict();
const persist: Persist<MyModel> = (model: Model<MyModel>): Promise<Model<MyModel>> => {};
const responseFactory = createResponseFactory();
const outputSchema = z.object({ id: z.string(), createdAt: z.date(), name: z.string() }).strict();
const encoder = createEncoder([createJsonTypeEncoder()]);
const serverRequestFactory = createServerRequestFactory();
const updateHandler = createUpdateHandler<Model>(
findOneById,
decoder,
inputSchema,
persist,
responseFactory,
outputSchema,
encoder
);
(async () => {
const request = serverRequestFactory('PUT', 'http://localhost:8080/api/pets/8ba9661b-ba7f-436b-bd25-c0606f911f7d');
const response = await updateHandler(request);
})();
createDeleteHandler
import { createDeleteHandler } from '@chubbyts/chubbyts-api/dist/handler/delete';
import { FindOneById, Remove } 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 findOneById: FindOneById<MyModel> = async (id: string): Promise<Model<MyModel>|undefined> => {};
const remove: Remove<MyModel> = (model: Model<MyModel>): Promise<void> => {};
const responseFactory = createResponseFactory();
const serverRequestFactory = createServerRequestFactory();
const deleteHandler = createDeleteHandler<Model>(
findOneById,
remove,
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
2024 Dominik Zogg