Skip to content

chubbyts/chubbyts-api

Repository files navigation

chubbyts-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

Requirements

Installation

Through NPM as @chubbyts/chubbyts-api.

npm i @chubbyts/chubbyts-api@^6.1.0

Usage

Handler

my-model.ts

import { z } from 'zod';
import type {
  EnrichedModel,
  EnrichedModelList,
  EnrichedModelListSchema,
  EnrichedModelSchema,
  InputModel,
  InputModelList,
  Model,
  ModelList,
  ModelListSchema,
  ModelSchema,
} from '@chubbyts/chubbyts-api/dist/model';
import {
  numberSchema,
  sortSchema,
  stringSchema,
  createEnrichedModelListSchema,
  createModelSchema,
  createModelListSchema,
  createEnrichedModelSchema,
} from '@chubbyts/chubbyts-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';
import { createJsonTypeEncoder }
  from '@chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder';
import {
  createResponseFactory,
  createServerRequestFactory,
} from '@chubbyts/chubbyts-http/dist/message-factory'; // any implementation can be used
import type { InputMyModelListSchema, InputMyModelSchema } from './my-model.js';
import {
  enrichedMyModelListSchema,
  inputMyModelListSchema,
} from './my-model.js';
import { ResolveModelList } from '@chubbyts/chubbyts-api/dist/repository';
import { InputModelList, ModelList } from '@chubbyts/chubbyts-api/dist/model';
import { createListHandler } from '@chubbyts/chubbyts-api/dist/handler/list';

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

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

(async () => {
  const request = serverRequestFactory('GET', 'http://localhost:8080/api/pets');
  const response = await listHandler(request);
})();

my-create-handler.ts

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 {
  createResponseFactory,
  createServerRequestFactory,
} from '@chubbyts/chubbyts-http/dist/message-factory'; // any implementation can be used
import type { InputMyModelSchema } from './my-model.js';
import { enrichedMyModelSchema, inputMyModelSchema } from './my-model.js';
import type { PersistModel } from '@chubbyts/chubbyts-api/dist/repository';
import type { Model } from '@chubbyts/chubbyts-api/dist/model';
import { createCreateHandler } from '@chubbyts/chubbyts-api/dist/handler/create';

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

const serverRequestFactory = createServerRequestFactory();

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

(async () => {
  const request = serverRequestFactory(
    'POST',
    'http://localhost:8080/api/pets',
  );
  const response = await createHandler(request);
})();

my-read-handler.ts

import { createEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder';
import { createJsonTypeEncoder }
  from '@chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder';
import { createResponseFactory, createServerRequestFactory }
  from '@chubbyts/chubbyts-http/dist/message-factory'; // any implementation can be used
import type { InputMyModel } from './my-model.js';
import { enrichedMyModelSchema } from './my-model.js';
import type { FindModelById } from '@chubbyts/chubbyts-api/dist/repository';
import type { Model } from '@chubbyts/chubbyts-api/dist/model';
import { createReadHandler } from '@chubbyts/chubbyts-api/dist/handler/read';

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

const serverRequestFactory = createServerRequestFactory();

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

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

my-update-handler.ts

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 {
  createResponseFactory,
  createServerRequestFactory,
} from '@chubbyts/chubbyts-http/dist/message-factory'; // any implementation can be used
import type { InputMyModelSchema } from './my-model.js';
import { enrichedMyModelSchema, inputMyModelSchema } from './my-model.js';
import type {
  FindModelById,
  PersistModel,
} from '@chubbyts/chubbyts-api/dist/repository';
import type { Model } from '@chubbyts/chubbyts-api/dist/model';
import { createUpdateHandler } from '@chubbyts/chubbyts-api/dist/handler/update';

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 responseFactory = createResponseFactory();
const encoder = createEncoder([createJsonTypeEncoder()]);

const serverRequestFactory = createServerRequestFactory();

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

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

my-delete-handler.ts

import {
  createResponseFactory,
  createServerRequestFactory,
} from '@chubbyts/chubbyts-http/dist/message-factory'; // any implementation can be used
import type { InputMyModelSchema } from './my-model.js';
import {
  FindModelById,
  RemoveModel,
} from '@chubbyts/chubbyts-api/dist/repository';
import { Model } from '@chubbyts/chubbyts-api/dist/model';
import { createDeleteHandler } from '@chubbyts/chubbyts-api/dist/handler/delete';

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

const serverRequestFactory = createServerRequestFactory();

const deleteHandler = createDeleteHandler(
  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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published