![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@navios/navios-zod
Advanced tools
Navios Zod
is a simple wrapper around zod
library to provide a more convenient way to work with zod
schemas.
Developers forget to use zod
to check the data before using it. This can lead to unexpected errors in the application. Navios Zod
provides a simple way to check the data before using it.
You cannot trust that API will return the data in the format you expect. Navios Zod
provides a simple way to check the data before using it.
npm install --save @navios/navios-zod zod navios
or
yarn add @navios/navios-zod zod navios
import { z } from 'zod'
import { createAPI } from '@navios/navios-zod'
const API = createAPI({
baseURL: 'https://example.com/api/',
})
const GetUserResponseSchema = z.object({
id: z.number(),
name: z.string(),
})
const getUser = API.declareEndpoint({
method: 'get',
url: 'user',
responseSchema: GetUserResponseSchema,
})
Or more complex example with request schema:
import { z } from 'zod'
import { declareAPI } from '@navios/navios-zod'
import { GetUsersResponseSchema } from './schemas/GetUsersResponseSchema.js'
const API = declareAPI({
useDiscriminatorResponse: true,
useWholeResponse: true,
})
const UpdateUserRequestSchema = z.object({
id: z.number(),
name: z.string(),
})
const UpdateUserResponseSchema = z.discriminatedUnion('status', [
z.object({
status: z.literal(200),
data: GetUsersResponseSchema,
}),
z.object({
status: z.literal(400),
data: z.object({
error: z.string(),
}),
}),
])
const updateUser = API.declareEndpoint({
method: 'PUT',
url: 'user/$userId',
requestSchema: UpdateUserRequestSchema,
responseSchema: UpdateUserResponseSchema,
})
// In another file you can set the API client
const client = create({
baseURL: 'https://example.com/api/',
headers: {
Authorization: 'Bearer token',
},
})
API.provideClient(client)
// Usage
const { data: updatedUserOrError, status } = await updateUser({
urlParams: {
userId: 1,
},
data: {
id: 1,
name: 'John Doe',
},
})
if (status === 200) {
console.log(updatedUserOrError)
} else {
console.error(updatedUserOrError)
}
declareAPI
declareAPI
is a function that creates an API object. It accepts an object with the following properties:
useDiscriminatorResponse
- if true
, the error response will be checked by the original responseSchema. Default is false
.useWholeResponse
- if true
, the whole response will be checked. Default is false
.The function returns an API object with the following methods:
declareEndpoint
- creates an endpoint with the specified options.declareEndpoint({
method: 'get' | 'post' | 'put' | 'delete' | 'patch',
url: string,
requestSchema?: z.ZodSchema<unknown>, // Only for POST, PUT, PATCH methods
responseSchema: z.ZodSchema<unknown>,
querySchema?: z.ZodSchema<unknown>,
})
provideClient
- sets the client for the API.provideClient(client: Navios)
createAPI
createAPI
is a wrapper around declareAPI
that creates an API object with default navios client.
const API = createAPI({
baseURL: string,
useDiscriminatorResponse?: boolean,
useWholeResponse?: boolean,
})
The function returns an API object with the same methods as declareAPI
.
FAQs
Unknown package
We found that @navios/navios-zod demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.