Function: defineContract()
defineContract<
T>(contract):T
Defined in: src/contract.ts:62
Defines a contract with full type inference.
Type Parameters
| Type Parameter |
|---|
T extends Contract |
Parameters
| Parameter | Type |
|---|---|
contract | T |
Returns
T
Example
ts
import { defineContract, defineEndpoint } from 'sorbus';
import { z } from 'zod';
const InvoiceSchema = z.object({
id: z.string(),
number: z.string(),
});
const index = defineEndpoint({
method: 'GET',
path: '/invoices',
response: {
body: z.object({
invoices: z.array(InvoiceSchema),
}),
},
});
const show = defineEndpoint({
method: 'GET',
path: '/invoices/:id',
pathParams: z.object({
id: z.string(),
}),
response: {
body: z.object({
invoice: InvoiceSchema,
}),
},
});
const contract = defineContract({
endpoints: {
invoices: {
index,
show,
},
},
error: z.object({
message: z.string(),
}),
});