Skip to content

chubbyts/chubbyts-undici-api

Repository files navigation

chubbyts-undici-api

CI Coverage Status Mutation testing badge npm-version

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

Description

A set of crud middlewares/handlers for chubbyts-undici-server.

Requirements

Installation

Through NPM as @chubbyts/chubbyts-undici-api.

npm i @chubbyts/chubbyts-undici-api@^1.1.1

Usage

Handler

my-model.ts

import { z } from 'zod';
import type {
  EnrichedModel,
  EnrichedModelList,
  EnrichedModelListSchema,
  EnrichedModelSchema,
  InputModel,
  InputModelList,
  Model,
  ModelList,
  ModelListSchema,
  ModelSchema,
} from '@chubbyts/chubbyts-undici-api/dist/model';
import {
  numberSchema,
  sortSchema,
  stringSchema,
  createEnrichedModelListSchema,
  createModelSchema,
  createModelListSchema,
  createEnrichedModelSchema,
} from '@chubbyts/chubbyts-undici-api/dist/model';

export const inputMyModelSchema = z
  .object({ name: stringSchema, value: stringSchema })
  .strict();

export type InputMyModelSchema = typeof inputMyModelSchema;

export type InputMyModel = InputModel<InputMyModelSchema>;

export const inputMyModelListSchema = z
  .object({
    offset: numberSchema.default(0),
    limit: numberSchema.default(20),
    filters: z.object({ name: stringSchema.optional() }).strict().default({}),
    sort: z.object({ name: sortSchema }).strict().default({}),
  })
  .strict();

export type InputMyModelListSchema = typeof inputMyModelListSchema;

export type InputMyModelList = InputModelList<InputMyModelListSchema>;

export type MyModelSchema = ModelSchema<InputMyModelSchema>;

export const myModelSchema: MyModelSchema =
  createModelSchema(inputMyModelSchema);

export type MyModel = Model<InputMyModelSchema>;

export type MyModelListSchema = ModelListSchema<
  InputMyModelSchema,
  InputMyModelListSchema
>;

export const myModelListSchema: MyModelListSchema = createModelListSchema(
  inputMyModelSchema,
  inputMyModelListSchema,
);

export type MyModelList = ModelList<InputMyModelSchema, InputMyModelListSchema>;

export type EnrichedMyModelSchema = EnrichedModelSchema<InputMyModelSchema>;

export const enrichedMyModelSchema: EnrichedMyModelSchema =
  createEnrichedModelSchema(inputMyModelSchema);

export type EnrichedMyModel = EnrichedModel<InputMyModelSchema>;

export type EnrichedMyModelListSchema = EnrichedModelListSchema<
  InputMyModelSchema,
  InputMyModelListSchema
>;

export const enrichedMyModelListSchema: EnrichedMyModelListSchema =
  createEnrichedModelListSchema(inputMyModelSchema, inputMyModelListSchema);

export type EnrichedMyModelList = EnrichedModelList<
  InputMyModelSchema,
  InputMyModelListSchema
>;

my-list-handler.ts

import { createEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/encoder';
import { createJsonTypeEncoder }
  from '@chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder';
import type { InputMyModelListSchema, InputMyModelSchema } from './my-model.js';
import {
  enrichedMyModelListSchema,
  inputMyModelListSchema,
} from './my-model.js';
import { ResolveModelList } from '@chubbyts/chubbyts-undici-api/dist/repository';
import { InputModelList, ModelList } from '@chubbyts/chubbyts-undici-api/dist/model';
import { createListHandler } from '@chubbyts/chubbyts-undici-api/dist/handler/list';
import { ServerRequest } from '@chubbyts/chubbyts-undici-server/dist/server';

const resolveModelList: ResolveModelList<
  InputMyModelSchema,
  InputMyModelListSchema
> = (
  modelList: InputModelList<InputMyModelListSchema>,
): Promise<ModelList<InputMyModelSchema>> => {};
const encoder = createEncoder([createJsonTypeEncoder()]);

const listHandler = createListHandler(
  inputMyModelListSchema,
  resolveModelList,
  enrichedMyModelListSchema,
  encoder,
);

(async () => {
  const serverRequest = new ServerRequest('http://localhost:8080/api/pets', {method: 'GET'});
  const response = await listHandler(serverRequest);
})();

my-create-handler.ts

import { createDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/decoder';
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/json-type-decoder';
import { createEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/encoder';
import { createJsonTypeEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder';
import type { InputMyModelSchema } from './my-model.js';
import { enrichedMyModelSchema, inputMyModelSchema } from './my-model.js';
import type { PersistModel } from '@chubbyts/chubbyts-undici-api/dist/repository';
import type { Model } from '@chubbyts/chubbyts-undici-api/dist/model';
import { createCreateHandler } from '@chubbyts/chubbyts-undici-api/dist/handler/create';
import { ServerRequest } from '@chubbyts/chubbyts-undici-server/dist/server';

const decoder = createDecoder([createJsonTypeDecoder()]);
const persistModel: PersistModel<InputMyModelSchema> = (
  model: Model<InputMyModelSchema>,
): Promise<Model<InputMyModelSchema>> => {};
const encoder = createEncoder([createJsonTypeEncoder()]);

const createHandler = createCreateHandler(
  decoder,
  inputMyModelSchema,
  persistModel,
  enrichedMyModelSchema,
  encoder,
);

(async () => {
  const serverRequest = new ServerRequest('http://localhost:8080/api/pets', {method: 'POST'});
  const response = await createHandler(serverRequest);
})();

my-read-handler.ts

import { createEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/encoder';
import { createJsonTypeEncoder }
  from '@chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder';
import type { InputMyModel } from './my-model.js';
import { enrichedMyModelSchema } from './my-model.js';
import type { FindModelById } from '@chubbyts/chubbyts-undici-api/dist/repository';
import type { Model } from '@chubbyts/chubbyts-undici-api/dist/model';
import { createReadHandler } from '@chubbyts/chubbyts-undici-api/dist/handler/read';
import { ServerRequest } from '@chubbyts/chubbyts-undici-server/dist/server';

const findModelById: FindModelById<InputMyModel> =
  async (id: string): Promise<Model<InputMyModel> | undefined> => {};
const encoder = createEncoder([createJsonTypeEncoder()]);

const readHandler = createReadHandler<InputMyModel>(
  findModelById,
  enrichedMyModelSchema,
  encoder
);

(async () => {
  const serverRequest = new ServerRequest(
    'http://localhost:8080/api/pets/8ba9661b-ba7f-436b-bd25-c0606f911f7d',
    { method: 'GET' }
  );
  const response = await readHandler(serverRequest);
})();

my-update-handler.ts

import { createDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/decoder';
import { createJsonTypeDecoder }
  from '@chubbyts/chubbyts-decode-encode/dist/decoder/json-type-decoder';
import { createEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/encoder';
import { createJsonTypeEncoder }
  from '@chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder';
import type { InputMyModelSchema } from './my-model.js';
import { enrichedMyModelSchema, inputMyModelSchema } from './my-model.js';
import type {
  FindModelById,
  PersistModel,
} from '@chubbyts/chubbyts-undici-api/dist/repository';
import type { Model } from '@chubbyts/chubbyts-undici-api/dist/model';
import { createUpdateHandler } from '@chubbyts/chubbyts-undici-api/dist/handler/update';
import { ServerRequest } from '@chubbyts/chubbyts-undici-server/dist/server';

const findModelById: FindModelById<InputMyModelSchema> = async (
  id: string,
): Promise<Model<InputMyModelSchema> | undefined> => {};
const decoder = createDecoder([createJsonTypeDecoder()]);
const persistModel: PersistModel<InputMyModelSchema> = (
  model: Model<InputMyModelSchema>,
): Promise<Model<InputMyModelSchema>> => {};
const encoder = createEncoder([createJsonTypeEncoder()]);

const updateHandler = createUpdateHandler(
  findModelById,
  decoder,
  inputMyModelSchema,
  persistModel,
  enrichedMyModelSchema,
  encoder,
);

(async () => {
  const serverRequest = new ServerRequest(
    'http://localhost:8080/api/pets/8ba9661b-ba7f-436b-bd25-c0606f911f7d',
    { method: 'PUT' }
  );
  const response = await updateHandler(serverRequest);
})();

my-delete-handler.ts

import type { InputMyModelSchema } from './my-model.js';
import {
  FindModelById,
  RemoveModel,
} from '@chubbyts/chubbyts-undici-api/dist/repository';
import { Model } from '@chubbyts/chubbyts-undici-api/dist/model';
import { createDeleteHandler } from '@chubbyts/chubbyts-undici-api/dist/handler/delete';
import { ServerRequest } from '@chubbyts/chubbyts-undici-server/dist/server';

const findModelById: FindModelById<InputMyModelSchema> = async (
  id: string,
): Promise<Model<InputMyModelSchema> | undefined> => {};
const removeModel: RemoveModel<InputMyModelSchema> = (
  model: Model<InputMyModelSchema>,
): Promise<void> => {};

const deleteHandler = createDeleteHandler(
  findModelById,
  removeModel,
);

(async () => {
  const serverRequest = new ServerRequest(
    'http://localhost:8080/api/pets/8ba9661b-ba7f-436b-bd25-c0606f911f7d',
    { method: 'DELETE' }
  );
  const response = await deleteHandler(serverRequest);
})();

my-typed-handler.ts

See typed if you want/need more flexibility and prefer a typed generic handler?

Middleware

createAcceptLanguageNegotiationMiddleware

createAcceptNegotiationMiddleware

createContentTypeNegotiationMiddleware

createErrorMiddleware

Copyright

2026 Dominik Zogg

About

A set of crud middlewares/handlers for chubbyts-undici-server.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published