Value Transforms
Transform values on input/output and handle nil/empty string conversion
API Definition
config/apis/swift_fox.rb
rb
# frozen_string_literal: true
Apiwork::API.define '/swift_fox' do
key_format :camel
export :openapi
export :apiwork
resources :contacts
endModels
app/models/swift_fox/contact.rb
rb
# frozen_string_literal: true
module SwiftFox
class Contact < ApplicationRecord
validates :name, presence: true
end
endDatabase Table
| Column | Type | Nullable | Default |
|---|---|---|---|
| id | string | ||
| created_at | datetime | ||
| string | ✓ | ||
| name | string | ||
| notes | string | ✓ | |
| phone | string | ✓ | |
| updated_at | datetime |
Representations
app/representations/swift_fox/contact_representation.rb
rb
# frozen_string_literal: true
module SwiftFox
class ContactRepresentation < Apiwork::Representation::Base
attribute :id
attribute :name, writable: true
attribute :email, decode: ->(v) { v&.downcase }, encode: ->(v) { v&.downcase }, writable: true
attribute :phone, empty: true, writable: true
attribute :notes, empty: true, writable: true
attribute :created_at
attribute :updated_at
end
endContracts
app/contracts/swift_fox/contact_contract.rb
rb
# frozen_string_literal: true
module SwiftFox
class ContactContract < Apiwork::Contract::Base
representation ContactRepresentation
end
endControllers
app/controllers/swift_fox/contacts_controller.rb
rb
# frozen_string_literal: true
module SwiftFox
class ContactsController < ApplicationController
before_action :set_contact, only: %i[show update destroy]
def index
contacts = Contact.all
expose contacts
end
def show
expose contact
end
def create
contact = Contact.create(contract.body[:contact])
expose contact
end
def update
contact.update(contract.body[:contact])
expose contact
end
def destroy
contact.destroy
expose contact
end
private
attr_reader :contact
def set_contact
@contact = Contact.find(params[:id])
end
end
endRequest Examples
Create with transforms
Request
http
POST /swift_fox/contacts
Content-Type: application/json
{
"contact": {
"name": "John Doe",
"email": "John.Doe@Example.COM",
"phone": "",
"notes": ""
}
}Response 201
json
{
"contact": {
"id": "a8683ee9-6e2e-525c-84e5-103a4b4230cb",
"name": "John Doe",
"email": "john.doe@example.com",
"phone": "",
"notes": "",
"createdAt": "2026-04-01T12:00:00.000Z",
"updatedAt": "2026-04-01T12:00:00.000Z"
}
}Show transformed data
Request
http
GET /swift_fox/contacts/a8683ee9-6e2e-525c-84e5-103a4b4230cbResponse 200
json
{
"contact": {
"id": "a8683ee9-6e2e-525c-84e5-103a4b4230cb",
"name": "Jane Doe",
"email": "jane@example.com",
"phone": "",
"notes": "",
"createdAt": "2026-04-01T12:00:00.000Z",
"updatedAt": "2026-04-01T12:00:00.000Z"
}
}Exports
OpenAPI
yml
---
openapi: 3.1.0
info:
title: "/swift_fox"
version: 1.0.0
paths:
"/contacts":
get:
operationId: contactsIndex
parameters:
- in: query
name: page
required: false
schema:
"$ref": "#/components/schemas/contactPage"
responses:
'200':
content:
application/json:
schema:
properties:
contacts:
items:
"$ref": "#/components/schemas/contact"
type: array
meta:
properties: {}
type: object
pagination:
"$ref": "#/components/schemas/offsetPagination"
type: object
required:
- contacts
- pagination
description: ''
post:
operationId: contactsCreate
requestBody:
content:
application/json:
schema:
properties:
contact:
"$ref": "#/components/schemas/contactCreatePayload"
type: object
required:
- contact
required: true
responses:
'200':
content:
application/json:
schema:
properties:
contact:
"$ref": "#/components/schemas/contact"
meta:
properties: {}
type: object
type: object
required:
- contact
description: ''
'422':
description: Unprocessable Entity
content:
application/json:
schema:
properties:
issues:
items:
"$ref": "#/components/schemas/error"
type: array
required:
- issues
type: object
"/contacts/{id}":
get:
operationId: contactsShow
parameters:
- in: path
name: id
required: true
schema:
type: string
responses:
'200':
content:
application/json:
schema:
properties:
contact:
"$ref": "#/components/schemas/contact"
meta:
properties: {}
type: object
type: object
required:
- contact
description: ''
patch:
operationId: contactsUpdate
parameters:
- in: path
name: id
required: true
schema:
type: string
requestBody:
content:
application/json:
schema:
properties:
contact:
"$ref": "#/components/schemas/contactUpdatePayload"
type: object
required:
- contact
required: true
responses:
'200':
content:
application/json:
schema:
properties:
contact:
"$ref": "#/components/schemas/contact"
meta:
properties: {}
type: object
type: object
required:
- contact
description: ''
'422':
description: Unprocessable Entity
content:
application/json:
schema:
properties:
issues:
items:
"$ref": "#/components/schemas/error"
type: array
required:
- issues
type: object
delete:
operationId: contactsDestroy
parameters:
- in: path
name: id
required: true
schema:
type: string
responses:
'204':
description: ''
components:
schemas:
contact:
properties:
createdAt:
type: string
format: date-time
email:
type:
- string
- 'null'
id:
type: string
name:
type: string
notes:
type: string
phone:
type: string
updatedAt:
type: string
format: date-time
type: object
required:
- createdAt
- email
- id
- name
- notes
- phone
- updatedAt
contactCreatePayload:
properties:
email:
type:
- string
- 'null'
default:
name:
type: string
notes:
type: string
default: ''
phone:
type: string
default: ''
type: object
required:
- name
contactPage:
properties:
number:
type: integer
minimum: 1
size:
type: integer
minimum: 1
maximum: 100
type: object
contactUpdatePayload:
properties:
email:
type:
- string
- 'null'
name:
type: string
notes:
type: string
phone:
type: string
type: object
error:
properties:
issues:
items:
"$ref": "#/components/schemas/errorIssue"
type: array
layer:
enum:
- http
- contract
- domain
type: string
type: object
required:
- issues
- layer
errorIssue:
properties:
code:
type: string
detail:
type: string
meta:
properties: {}
type: object
path:
items:
oneOf:
- type: string
- type: integer
type: array
pointer:
type: string
type: object
required:
- code
- detail
- meta
- path
- pointer
offsetPagination:
properties:
current:
type: integer
items:
type: integer
next:
type:
- integer
- 'null'
prev:
type:
- integer
- 'null'
total:
type: integer
type: object
required:
- current
- items
- totalApiwork
json
{
"base_path": "/swift_fox",
"enums": [
{
"deprecated": false,
"description": null,
"example": null,
"name": "error_layer",
"scope": null,
"values": [
"http",
"contract",
"domain"
]
}
],
"error_codes": [
{
"description": "Unprocessable Entity",
"name": "unprocessable_entity",
"status": 422
}
],
"fingerprint": "f70bea5be6982115",
"info": null,
"locales": [],
"resources": [
{
"actions": [
{
"name": "contacts.index",
"deprecated": false,
"description": null,
"method": "get",
"operation_id": null,
"path": "/contacts",
"raises": [],
"request": {
"body": [],
"description": null,
"query": [
{
"name": "page",
"deprecated": false,
"description": null,
"nullable": false,
"optional": true,
"type": "reference",
"reference": "contact_page"
}
]
},
"response": {
"body": {
"deprecated": null,
"description": null,
"nullable": null,
"optional": null,
"type": "object",
"partial": null,
"shape": [
{
"name": "contacts",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "array",
"example": null,
"max": null,
"min": null,
"of": {
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "reference",
"reference": "contact"
}
},
{
"name": "meta",
"deprecated": false,
"description": null,
"nullable": false,
"optional": true,
"type": "object",
"partial": false,
"shape": []
},
{
"name": "pagination",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "reference",
"reference": "offset_pagination"
}
]
},
"description": null,
"no_content": false
},
"summary": null,
"tags": []
},
{
"name": "contacts.show",
"deprecated": false,
"description": null,
"method": "get",
"operation_id": null,
"path": "/contacts/:id",
"raises": [],
"request": {
"body": [],
"description": null,
"query": []
},
"response": {
"body": {
"deprecated": null,
"description": null,
"nullable": null,
"optional": null,
"type": "object",
"partial": null,
"shape": [
{
"name": "contact",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "reference",
"reference": "contact"
},
{
"name": "meta",
"deprecated": false,
"description": null,
"nullable": false,
"optional": true,
"type": "object",
"partial": false,
"shape": []
}
]
},
"description": null,
"no_content": false
},
"summary": null,
"tags": []
},
{
"name": "contacts.create",
"deprecated": false,
"description": null,
"method": "post",
"operation_id": null,
"path": "/contacts",
"raises": [
"unprocessable_entity"
],
"request": {
"body": [
{
"name": "contact",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "reference",
"reference": "contact_create_payload"
}
],
"description": null,
"query": []
},
"response": {
"body": {
"deprecated": null,
"description": null,
"nullable": null,
"optional": null,
"type": "object",
"partial": null,
"shape": [
{
"name": "contact",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "reference",
"reference": "contact"
},
{
"name": "meta",
"deprecated": false,
"description": null,
"nullable": false,
"optional": true,
"type": "object",
"partial": false,
"shape": []
}
]
},
"description": null,
"no_content": false
},
"summary": null,
"tags": []
},
{
"name": "contacts.update",
"deprecated": false,
"description": null,
"method": "patch",
"operation_id": null,
"path": "/contacts/:id",
"raises": [
"unprocessable_entity"
],
"request": {
"body": [
{
"name": "contact",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "reference",
"reference": "contact_update_payload"
}
],
"description": null,
"query": []
},
"response": {
"body": {
"deprecated": null,
"description": null,
"nullable": null,
"optional": null,
"type": "object",
"partial": null,
"shape": [
{
"name": "contact",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "reference",
"reference": "contact"
},
{
"name": "meta",
"deprecated": false,
"description": null,
"nullable": false,
"optional": true,
"type": "object",
"partial": false,
"shape": []
}
]
},
"description": null,
"no_content": false
},
"summary": null,
"tags": []
},
{
"name": "contacts.destroy",
"deprecated": false,
"description": null,
"method": "delete",
"operation_id": null,
"path": "/contacts/:id",
"raises": [],
"request": {
"body": [],
"description": null,
"query": []
},
"response": {
"body": null,
"description": null,
"no_content": true
},
"summary": null,
"tags": []
}
],
"identifier": "contacts",
"name": "contacts",
"parent_identifiers": [],
"path": "contacts",
"resources": [],
"scope": "contact"
}
],
"types": [
{
"recursive": false,
"deprecated": false,
"description": null,
"example": null,
"name": "contact",
"scope": "contact",
"type": "object",
"extends": [],
"shape": [
{
"name": "createdAt",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "datetime",
"example": null
},
{
"name": "email",
"deprecated": false,
"description": null,
"nullable": true,
"optional": false,
"type": "string",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "id",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "string",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "name",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "string",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "notes",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "string",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "phone",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "string",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "updatedAt",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "datetime",
"example": null
}
]
},
{
"recursive": false,
"deprecated": false,
"description": null,
"example": null,
"name": "contact_create_payload",
"scope": "contact",
"type": "object",
"extends": [],
"shape": [
{
"name": "email",
"deprecated": false,
"description": null,
"nullable": true,
"optional": true,
"type": "string",
"default": null,
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "name",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "string",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "notes",
"deprecated": false,
"description": null,
"nullable": false,
"optional": true,
"type": "string",
"default": "",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "phone",
"deprecated": false,
"description": null,
"nullable": false,
"optional": true,
"type": "string",
"default": "",
"example": null,
"format": null,
"max": null,
"min": null
}
]
},
{
"recursive": false,
"deprecated": false,
"description": null,
"example": null,
"name": "contact_page",
"scope": "contact",
"type": "object",
"extends": [],
"shape": [
{
"name": "number",
"deprecated": false,
"description": null,
"nullable": false,
"optional": true,
"type": "integer",
"example": null,
"format": null,
"max": null,
"min": 1
},
{
"name": "size",
"deprecated": false,
"description": null,
"nullable": false,
"optional": true,
"type": "integer",
"example": null,
"format": null,
"max": 100,
"min": 1
}
]
},
{
"recursive": false,
"deprecated": false,
"description": null,
"example": null,
"name": "contact_update_payload",
"scope": "contact",
"type": "object",
"extends": [],
"shape": [
{
"name": "email",
"deprecated": false,
"description": null,
"nullable": true,
"optional": true,
"type": "string",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "name",
"deprecated": false,
"description": null,
"nullable": false,
"optional": true,
"type": "string",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "notes",
"deprecated": false,
"description": null,
"nullable": false,
"optional": true,
"type": "string",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "phone",
"deprecated": false,
"description": null,
"nullable": false,
"optional": true,
"type": "string",
"example": null,
"format": null,
"max": null,
"min": null
}
]
},
{
"recursive": false,
"deprecated": false,
"description": null,
"example": null,
"name": "error_issue",
"scope": null,
"type": "object",
"extends": [],
"shape": [
{
"name": "code",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "string",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "detail",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "string",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "meta",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "object",
"partial": false,
"shape": []
},
{
"name": "path",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "array",
"example": null,
"max": null,
"min": null,
"of": {
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "union",
"discriminator": "",
"variants": [
{
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "string",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "integer",
"example": null,
"format": null,
"max": null,
"min": null
}
]
}
},
{
"name": "pointer",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "string",
"example": null,
"format": null,
"max": null,
"min": null
}
]
},
{
"recursive": false,
"deprecated": false,
"description": null,
"example": null,
"name": "offset_pagination",
"scope": null,
"type": "object",
"extends": [],
"shape": [
{
"name": "current",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "integer",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "items",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "integer",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "next",
"deprecated": false,
"description": null,
"nullable": true,
"optional": true,
"type": "integer",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "prev",
"deprecated": false,
"description": null,
"nullable": true,
"optional": true,
"type": "integer",
"example": null,
"format": null,
"max": null,
"min": null
},
{
"name": "total",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "integer",
"example": null,
"format": null,
"max": null,
"min": null
}
]
},
{
"recursive": false,
"deprecated": false,
"description": null,
"example": null,
"name": "error",
"scope": null,
"type": "object",
"extends": [],
"shape": [
{
"name": "issues",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "array",
"example": null,
"max": null,
"min": null,
"of": {
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "reference",
"reference": "error_issue"
}
},
{
"name": "layer",
"deprecated": false,
"description": null,
"nullable": false,
"optional": false,
"type": "reference",
"reference": "error_layer"
}
]
}
]
}Codegen
TypeScript
ts
export type ErrorLayer = 'http' | 'contract' | 'domain';
export interface ErrorIssue {
code: string;
detail: string;
meta: Record<string, unknown>;
path: string | number[];
pointer: string;
}
export interface OffsetPagination {
current: number;
items: number;
next?: number | null;
prev?: number | null;
total: number;
}
export interface Error {
issues: ErrorIssue[];
layer: ErrorLayer;
}ts
export interface Contact {
createdAt: string;
email: string | null;
id: string;
name: string;
notes: string;
phone: string;
updatedAt: string;
}
export interface ContactCreatePayload {
email?: string | null;
name: string;
notes?: string;
phone?: string;
}
export interface ContactPage {
number?: number;
size?: number;
}
export interface ContactUpdatePayload {
email?: string | null;
name?: string;
notes?: string;
phone?: string;
}ts
export * from './contact';ts
import type { OffsetPagination } from '../api';
import type {
Contact,
ContactCreatePayload,
ContactPage,
ContactUpdatePayload,
} from '../domains/contact';
export type ContactsIndexMethod = 'GET';
export type ContactsIndexPath = '/contacts';
export interface ContactsIndexRequestQuery {
page?: ContactPage;
}
export type ContactsIndexResponseBody = {
contacts: Contact[];
meta?: Record<string, unknown>;
pagination: OffsetPagination;
};
export interface ContactsIndexRequest {
query: ContactsIndexRequestQuery;
}
export interface ContactsIndexResponse {
body: ContactsIndexResponseBody;
}
export interface ContactsIndex {
method: ContactsIndexMethod;
path: ContactsIndexPath;
request: ContactsIndexRequest;
response: ContactsIndexResponse;
}
export type ContactsShowMethod = 'GET';
export type ContactsShowPath = '/contacts/:id';
export interface ContactsShowPathParams {
id: string;
}
export type ContactsShowResponseBody = {
contact: Contact;
meta?: Record<string, unknown>;
};
export interface ContactsShowResponse {
body: ContactsShowResponseBody;
}
export interface ContactsShow {
method: ContactsShowMethod;
path: ContactsShowPath;
pathParams: ContactsShowPathParams;
response: ContactsShowResponse;
}
export type ContactsCreateMethod = 'POST';
export type ContactsCreatePath = '/contacts';
export interface ContactsCreateRequestBody {
contact: ContactCreatePayload;
}
export type ContactsCreateResponseBody = {
contact: Contact;
meta?: Record<string, unknown>;
};
export interface ContactsCreateRequest {
body: ContactsCreateRequestBody;
}
export interface ContactsCreateResponse {
body: ContactsCreateResponseBody;
}
export type ContactsCreateErrors = 422;
export interface ContactsCreate {
errors: ContactsCreateErrors;
method: ContactsCreateMethod;
path: ContactsCreatePath;
request: ContactsCreateRequest;
response: ContactsCreateResponse;
}
export type ContactsUpdateMethod = 'PATCH';
export type ContactsUpdatePath = '/contacts/:id';
export interface ContactsUpdatePathParams {
id: string;
}
export interface ContactsUpdateRequestBody {
contact: ContactUpdatePayload;
}
export type ContactsUpdateResponseBody = {
contact: Contact;
meta?: Record<string, unknown>;
};
export interface ContactsUpdateRequest {
body: ContactsUpdateRequestBody;
}
export interface ContactsUpdateResponse {
body: ContactsUpdateResponseBody;
}
export type ContactsUpdateErrors = 422;
export interface ContactsUpdate {
errors: ContactsUpdateErrors;
method: ContactsUpdateMethod;
path: ContactsUpdatePath;
pathParams: ContactsUpdatePathParams;
request: ContactsUpdateRequest;
response: ContactsUpdateResponse;
}
export type ContactsDestroyMethod = 'DELETE';
export type ContactsDestroyPath = '/contacts/:id';
export interface ContactsDestroyPathParams {
id: string;
}
export interface ContactsDestroy {
method: ContactsDestroyMethod;
path: ContactsDestroyPath;
pathParams: ContactsDestroyPathParams;
}ts
export * from './contacts';Zod
ts
import * as z from 'zod';
export const ErrorLayerSchema = z.enum(['contract', 'domain', 'http']);
export const ErrorIssueSchema = z.object({
code: z.string(),
detail: z.string(),
meta: z.record(z.string(), z.unknown()),
path: z.array(z.union([z.string(), z.number().int()])),
pointer: z.string(),
});
export const OffsetPaginationSchema = z.object({
current: z.number().int(),
items: z.number().int(),
next: z.number().int().nullable().optional(),
prev: z.number().int().nullable().optional(),
total: z.number().int(),
});
export const ErrorSchema = z.object({
issues: z.array(ErrorIssueSchema),
layer: ErrorLayerSchema,
});
export type ErrorLayer = 'contract' | 'domain' | 'http';
export interface ErrorIssue {
code: string;
detail: string;
meta: Record<string, unknown>;
path: string | number[];
pointer: string;
}
export interface OffsetPagination {
current: number;
items: number;
next?: number | null;
prev?: number | null;
total: number;
}
export interface Error {
issues: ErrorIssue[];
layer: ErrorLayer;
}ts
import * as z from 'zod';
export const ContactSchema = z.object({
createdAt: z.iso.datetime(),
email: z.string().nullable(),
id: z.string(),
name: z.string(),
notes: z.string(),
phone: z.string(),
updatedAt: z.iso.datetime(),
});
export const ContactCreatePayloadSchema = z.object({
email: z.string().nullable().default(null),
name: z.string(),
notes: z.string().default(''),
phone: z.string().default(''),
});
export const ContactPageSchema = z.object({
number: z.number().int().min(1).optional(),
size: z.number().int().min(1).max(100).optional(),
});
export const ContactUpdatePayloadSchema = z.object({
email: z.string().nullable().optional(),
name: z.string().optional(),
notes: z.string().optional(),
phone: z.string().optional(),
});
export interface Contact {
createdAt: string;
email: string | null;
id: string;
name: string;
notes: string;
phone: string;
updatedAt: string;
}
export interface ContactCreatePayload {
email?: string | null;
name: string;
notes?: string;
phone?: string;
}
export interface ContactPage {
number?: number;
size?: number;
}
export interface ContactUpdatePayload {
email?: string | null;
name?: string;
notes?: string;
phone?: string;
}ts
export * from './contact';ts
import type { OffsetPagination } from '../api';
import type {
Contact,
ContactCreatePayload,
ContactPage,
ContactUpdatePayload,
} from '../domains/contact';
import * as z from 'zod';
import { OffsetPaginationSchema } from '../api';
import {
ContactCreatePayloadSchema,
ContactPageSchema,
ContactSchema,
ContactUpdatePayloadSchema,
} from '../domains/contact';
export const ContactsIndexRequestQuerySchema = z.object({
page: ContactPageSchema.optional(),
});
export const ContactsIndexResponseBodySchema = z.object({
contacts: z.array(ContactSchema),
meta: z.record(z.string(), z.unknown()).optional(),
pagination: OffsetPaginationSchema,
});
export const ContactsShowPathParamsSchema = z.object({ id: z.string() });
export const ContactsShowResponseBodySchema = z.object({
contact: ContactSchema,
meta: z.record(z.string(), z.unknown()).optional(),
});
export const ContactsCreateRequestBodySchema = z.object({
contact: ContactCreatePayloadSchema,
});
export const ContactsCreateResponseBodySchema = z.object({
contact: ContactSchema,
meta: z.record(z.string(), z.unknown()).optional(),
});
export const ContactsUpdatePathParamsSchema = z.object({ id: z.string() });
export const ContactsUpdateRequestBodySchema = z.object({
contact: ContactUpdatePayloadSchema,
});
export const ContactsUpdateResponseBodySchema = z.object({
contact: ContactSchema,
meta: z.record(z.string(), z.unknown()).optional(),
});
export const ContactsDestroyPathParamsSchema = z.object({ id: z.string() });
export type ContactsIndexMethod = 'GET';
export type ContactsIndexPath = '/contacts';
export interface ContactsIndexRequestQuery {
page?: ContactPage;
}
export type ContactsIndexResponseBody = {
contacts: Contact[];
meta?: Record<string, unknown>;
pagination: OffsetPagination;
};
export interface ContactsIndexRequest {
query: ContactsIndexRequestQuery;
}
export interface ContactsIndexResponse {
body: ContactsIndexResponseBody;
}
export interface ContactsIndex {
method: ContactsIndexMethod;
path: ContactsIndexPath;
request: ContactsIndexRequest;
response: ContactsIndexResponse;
}
export type ContactsShowMethod = 'GET';
export type ContactsShowPath = '/contacts/:id';
export interface ContactsShowPathParams {
id: string;
}
export type ContactsShowResponseBody = {
contact: Contact;
meta?: Record<string, unknown>;
};
export interface ContactsShowResponse {
body: ContactsShowResponseBody;
}
export interface ContactsShow {
method: ContactsShowMethod;
path: ContactsShowPath;
pathParams: ContactsShowPathParams;
response: ContactsShowResponse;
}
export type ContactsCreateMethod = 'POST';
export type ContactsCreatePath = '/contacts';
export interface ContactsCreateRequestBody {
contact: ContactCreatePayload;
}
export type ContactsCreateResponseBody = {
contact: Contact;
meta?: Record<string, unknown>;
};
export interface ContactsCreateRequest {
body: ContactsCreateRequestBody;
}
export interface ContactsCreateResponse {
body: ContactsCreateResponseBody;
}
export type ContactsCreateErrors = 422;
export interface ContactsCreate {
errors: ContactsCreateErrors;
method: ContactsCreateMethod;
path: ContactsCreatePath;
request: ContactsCreateRequest;
response: ContactsCreateResponse;
}
export type ContactsUpdateMethod = 'PATCH';
export type ContactsUpdatePath = '/contacts/:id';
export interface ContactsUpdatePathParams {
id: string;
}
export interface ContactsUpdateRequestBody {
contact: ContactUpdatePayload;
}
export type ContactsUpdateResponseBody = {
contact: Contact;
meta?: Record<string, unknown>;
};
export interface ContactsUpdateRequest {
body: ContactsUpdateRequestBody;
}
export interface ContactsUpdateResponse {
body: ContactsUpdateResponseBody;
}
export type ContactsUpdateErrors = 422;
export interface ContactsUpdate {
errors: ContactsUpdateErrors;
method: ContactsUpdateMethod;
path: ContactsUpdatePath;
pathParams: ContactsUpdatePathParams;
request: ContactsUpdateRequest;
response: ContactsUpdateResponse;
}
export type ContactsDestroyMethod = 'DELETE';
export type ContactsDestroyPath = '/contacts/:id';
export interface ContactsDestroyPathParams {
id: string;
}
export interface ContactsDestroy {
method: ContactsDestroyMethod;
path: ContactsDestroyPath;
pathParams: ContactsDestroyPathParams;
}ts
export * from './contacts';Sorbus
ts
import * as z from 'zod';
export const ErrorLayerSchema = z.enum(['contract', 'domain', 'http']);
export const ErrorIssueSchema = z.object({
code: z.string(),
detail: z.string(),
meta: z.record(z.string(), z.unknown()),
path: z.array(z.union([z.string(), z.number().int()])),
pointer: z.string(),
});
export const OffsetPaginationSchema = z.object({
current: z.number().int(),
items: z.number().int(),
next: z.number().int().nullable().optional(),
prev: z.number().int().nullable().optional(),
total: z.number().int(),
});
export const ErrorSchema = z.object({
issues: z.array(ErrorIssueSchema),
layer: ErrorLayerSchema,
});
export type ErrorLayer = 'contract' | 'domain' | 'http';
export interface ErrorIssue {
code: string;
detail: string;
meta: Record<string, unknown>;
path: string | number[];
pointer: string;
}
export interface OffsetPagination {
current: number;
items: number;
next?: number | null;
prev?: number | null;
total: number;
}
export interface Error {
issues: ErrorIssue[];
layer: ErrorLayer;
}ts
import type { ContactsOperationTree } from './endpoints';
import { createClientFactory } from 'sorbus';
import { contract } from './contract';
export interface Client {
contacts: ContactsOperationTree;
}
export const createClient = createClientFactory<Client>(contract);ts
import { ErrorSchema } from './api';
import { contacts } from './endpoints';
export const contract = {
endpoints: {
contacts,
},
error: ErrorSchema,
} as const;ts
import * as z from 'zod';
export const ContactSchema = z.object({
createdAt: z.iso.datetime(),
email: z.string().nullable(),
id: z.string(),
name: z.string(),
notes: z.string(),
phone: z.string(),
updatedAt: z.iso.datetime(),
});
export const ContactCreatePayloadSchema = z.object({
email: z.string().nullable().default(null),
name: z.string(),
notes: z.string().default(''),
phone: z.string().default(''),
});
export const ContactPageSchema = z.object({
number: z.number().int().min(1).optional(),
size: z.number().int().min(1).max(100).optional(),
});
export const ContactUpdatePayloadSchema = z.object({
email: z.string().nullable().optional(),
name: z.string().optional(),
notes: z.string().optional(),
phone: z.string().optional(),
});
export interface Contact {
createdAt: string;
email: string | null;
id: string;
name: string;
notes: string;
phone: string;
updatedAt: string;
}
export interface ContactCreatePayload {
email?: string | null;
name: string;
notes?: string;
phone?: string;
}
export interface ContactPage {
number?: number;
size?: number;
}
export interface ContactUpdatePayload {
email?: string | null;
name?: string;
notes?: string;
phone?: string;
}ts
export * from './contact';ts
import type { Operation } from 'sorbus';
import type { OffsetPagination } from '../api';
import type {
Contact,
ContactCreatePayload,
ContactPage,
ContactUpdatePayload,
} from '../domains/contact';
import * as z from 'zod';
import { OffsetPaginationSchema } from '../api';
import {
ContactCreatePayloadSchema,
ContactPageSchema,
ContactSchema,
ContactUpdatePayloadSchema,
} from '../domains/contact';
export const ContactsIndexRequestQuerySchema = z.object({
page: ContactPageSchema.optional(),
});
export const ContactsIndexResponseBodySchema = z.object({
contacts: z.array(ContactSchema),
meta: z.record(z.string(), z.unknown()).optional(),
pagination: OffsetPaginationSchema,
});
export const ContactsShowPathParamsSchema = z.object({ id: z.string() });
export const ContactsShowResponseBodySchema = z.object({
contact: ContactSchema,
meta: z.record(z.string(), z.unknown()).optional(),
});
export const ContactsCreateRequestBodySchema = z.object({
contact: ContactCreatePayloadSchema,
});
export const ContactsCreateResponseBodySchema = z.object({
contact: ContactSchema,
meta: z.record(z.string(), z.unknown()).optional(),
});
export const ContactsUpdatePathParamsSchema = z.object({ id: z.string() });
export const ContactsUpdateRequestBodySchema = z.object({
contact: ContactUpdatePayloadSchema,
});
export const ContactsUpdateResponseBodySchema = z.object({
contact: ContactSchema,
meta: z.record(z.string(), z.unknown()).optional(),
});
export const ContactsDestroyPathParamsSchema = z.object({ id: z.string() });
export type ContactsIndexMethod = 'GET';
export type ContactsIndexPath = '/contacts';
export interface ContactsIndexRequestQuery {
page?: ContactPage;
}
export type ContactsIndexResponseBody = {
contacts: Contact[];
meta?: Record<string, unknown>;
pagination: OffsetPagination;
};
export interface ContactsIndexRequest {
query: ContactsIndexRequestQuery;
}
export interface ContactsIndexResponse {
body: ContactsIndexResponseBody;
}
export interface ContactsIndex {
method: ContactsIndexMethod;
path: ContactsIndexPath;
request: ContactsIndexRequest;
response: ContactsIndexResponse;
}
export type ContactsShowMethod = 'GET';
export type ContactsShowPath = '/contacts/:id';
export interface ContactsShowPathParams {
id: string;
}
export type ContactsShowResponseBody = {
contact: Contact;
meta?: Record<string, unknown>;
};
export interface ContactsShowResponse {
body: ContactsShowResponseBody;
}
export interface ContactsShow {
method: ContactsShowMethod;
path: ContactsShowPath;
pathParams: ContactsShowPathParams;
response: ContactsShowResponse;
}
export type ContactsCreateMethod = 'POST';
export type ContactsCreatePath = '/contacts';
export interface ContactsCreateRequestBody {
contact: ContactCreatePayload;
}
export type ContactsCreateResponseBody = {
contact: Contact;
meta?: Record<string, unknown>;
};
export interface ContactsCreateRequest {
body: ContactsCreateRequestBody;
}
export interface ContactsCreateResponse {
body: ContactsCreateResponseBody;
}
export type ContactsCreateErrors = 422;
export interface ContactsCreate {
errors: ContactsCreateErrors;
method: ContactsCreateMethod;
path: ContactsCreatePath;
request: ContactsCreateRequest;
response: ContactsCreateResponse;
}
export type ContactsUpdateMethod = 'PATCH';
export type ContactsUpdatePath = '/contacts/:id';
export interface ContactsUpdatePathParams {
id: string;
}
export interface ContactsUpdateRequestBody {
contact: ContactUpdatePayload;
}
export type ContactsUpdateResponseBody = {
contact: Contact;
meta?: Record<string, unknown>;
};
export interface ContactsUpdateRequest {
body: ContactsUpdateRequestBody;
}
export interface ContactsUpdateResponse {
body: ContactsUpdateResponseBody;
}
export type ContactsUpdateErrors = 422;
export interface ContactsUpdate {
errors: ContactsUpdateErrors;
method: ContactsUpdateMethod;
path: ContactsUpdatePath;
pathParams: ContactsUpdatePathParams;
request: ContactsUpdateRequest;
response: ContactsUpdateResponse;
}
export type ContactsDestroyMethod = 'DELETE';
export type ContactsDestroyPath = '/contacts/:id';
export interface ContactsDestroyPathParams {
id: string;
}
export interface ContactsDestroy {
method: ContactsDestroyMethod;
path: ContactsDestroyPath;
pathParams: ContactsDestroyPathParams;
}
export const contacts = {
create: {
errors: [422],
method: 'POST',
path: '/contacts',
request: {
body: ContactsCreateRequestBodySchema,
},
response: {
body: ContactsCreateResponseBodySchema,
},
},
destroy: {
method: 'DELETE',
path: '/contacts/:id',
pathParams: ContactsDestroyPathParamsSchema,
},
index: {
method: 'GET',
path: '/contacts',
request: {
query: ContactsIndexRequestQuerySchema,
},
response: {
body: ContactsIndexResponseBodySchema,
},
},
show: {
method: 'GET',
path: '/contacts/:id',
pathParams: ContactsShowPathParamsSchema,
response: {
body: ContactsShowResponseBodySchema,
},
},
update: {
errors: [422],
method: 'PATCH',
path: '/contacts/:id',
pathParams: ContactsUpdatePathParamsSchema,
request: {
body: ContactsUpdateRequestBodySchema,
},
response: {
body: ContactsUpdateResponseBodySchema,
},
},
} as const;
export interface ContactsOperationTree {
create: Operation<ContactsCreate>;
destroy: Operation<ContactsDestroy>;
index: Operation<ContactsIndex>;
show: Operation<ContactsShow>;
update: Operation<ContactsUpdate>;
}ts
export * from './contacts';