@navios/navios-zod
Advanced tools
Comparing version 0.4.0 to 0.5.0-alpha.0
import * as navios from 'navios'; | ||
import { NaviosConfig, HttpMethod, NaviosRequestConfig, Navios } from 'navios'; | ||
import * as zod from 'zod'; | ||
import { z, AnyZodObject, ZodDiscriminatedUnion } from 'zod'; | ||
@@ -108,135 +107,5 @@ | ||
declare function createAPI(config: APIConfig): { | ||
get: (options: NoInfer<Omit<EndpointConfig, "method">>) => (request?: { | ||
headers?: Record<string, string> | undefined; | ||
baseURL?: string | undefined; | ||
validateStatus?: ((status: number) => boolean) | undefined; | ||
body?: BodyInit | null | undefined; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method?: string | undefined; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
} | undefined) => Promise<any>; | ||
post: (options: NoInfer<Omit<Omit<EndpointWithDataConfig, "requestSchema"> & { | ||
requestSchema?: zod.AnyZodObject | undefined; | ||
}, "method">>) => (request?: { | ||
headers?: Record<string, string> | undefined; | ||
baseURL?: string | undefined; | ||
validateStatus?: ((status: number) => boolean) | undefined; | ||
body?: BodyInit | null | undefined; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method?: string | undefined; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
} | undefined) => Promise<any>; | ||
put: (options: NoInfer<Omit<Omit<EndpointWithDataConfig, "requestSchema"> & { | ||
requestSchema?: zod.AnyZodObject | undefined; | ||
}, "method">>) => (request?: { | ||
headers?: Record<string, string> | undefined; | ||
baseURL?: string | undefined; | ||
validateStatus?: ((status: number) => boolean) | undefined; | ||
body?: BodyInit | null | undefined; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method?: string | undefined; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
} | undefined) => Promise<any>; | ||
delete: (options: NoInfer<Omit<EndpointConfig, "method">>) => (request?: { | ||
headers?: Record<string, string> | undefined; | ||
baseURL?: string | undefined; | ||
validateStatus?: ((status: number) => boolean) | undefined; | ||
body?: BodyInit | null | undefined; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method?: string | undefined; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
} | undefined) => Promise<any>; | ||
patch: (options: NoInfer<Omit<Omit<EndpointWithDataConfig, "requestSchema"> & { | ||
requestSchema?: zod.AnyZodObject | undefined; | ||
}, "method">>) => (request?: { | ||
headers?: Record<string, string> | undefined; | ||
baseURL?: string | undefined; | ||
validateStatus?: ((status: number) => boolean) | undefined; | ||
body?: BodyInit | null | undefined; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method?: string | undefined; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
} | undefined) => Promise<any>; | ||
head: (options: NoInfer<Omit<EndpointConfig, "method">>) => (request?: { | ||
headers?: Record<string, string> | undefined; | ||
baseURL?: string | undefined; | ||
validateStatus?: ((status: number) => boolean) | undefined; | ||
body?: BodyInit | null | undefined; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method?: string | undefined; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
} | undefined) => Promise<any>; | ||
options: (options: NoInfer<Omit<EndpointConfig, "method">>) => (request?: { | ||
headers?: Record<string, string> | undefined; | ||
baseURL?: string | undefined; | ||
validateStatus?: ((status: number) => boolean) | undefined; | ||
body?: BodyInit | null | undefined; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method?: string | undefined; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
} | undefined) => Promise<any>; | ||
declareEndpoint: <Config extends EndpointConfig | EndpointWithDataConfig>(options: Config) => Util_FlatType<Endpoint<Config>>; | ||
declareEndpoint: <Config extends EndpointConfig | EndpointWithDataConfig>(options: Config) => Util_FlatType<Endpoint<Config>> & { | ||
config: Config; | ||
}; | ||
declareBlobEndpoint: <Config_1 extends BlobEndpointConfig>(options: Config_1) => Util_FlatType<BlobRequestEndpoint<Config_1>>; | ||
@@ -248,135 +117,5 @@ provideClient: (newClient: navios.Navios) => void; | ||
declare function declareAPI({ useDiscriminatorResponse, useWholeResponse, }?: DeclareAPIConfig): { | ||
get: (options: NoInfer<Omit<EndpointConfig, "method">>) => (request?: { | ||
headers?: Record<string, string> | undefined; | ||
baseURL?: string | undefined; | ||
validateStatus?: ((status: number) => boolean) | undefined; | ||
body?: BodyInit | null | undefined; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method?: string | undefined; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
} | undefined) => Promise<any>; | ||
post: (options: NoInfer<Omit<Omit<EndpointWithDataConfig, "requestSchema"> & { | ||
requestSchema?: AnyZodObject | undefined; | ||
}, "method">>) => (request?: { | ||
headers?: Record<string, string> | undefined; | ||
baseURL?: string | undefined; | ||
validateStatus?: ((status: number) => boolean) | undefined; | ||
body?: BodyInit | null | undefined; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method?: string | undefined; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
} | undefined) => Promise<any>; | ||
put: (options: NoInfer<Omit<Omit<EndpointWithDataConfig, "requestSchema"> & { | ||
requestSchema?: AnyZodObject | undefined; | ||
}, "method">>) => (request?: { | ||
headers?: Record<string, string> | undefined; | ||
baseURL?: string | undefined; | ||
validateStatus?: ((status: number) => boolean) | undefined; | ||
body?: BodyInit | null | undefined; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method?: string | undefined; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
} | undefined) => Promise<any>; | ||
delete: (options: NoInfer<Omit<EndpointConfig, "method">>) => (request?: { | ||
headers?: Record<string, string> | undefined; | ||
baseURL?: string | undefined; | ||
validateStatus?: ((status: number) => boolean) | undefined; | ||
body?: BodyInit | null | undefined; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method?: string | undefined; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
} | undefined) => Promise<any>; | ||
patch: (options: NoInfer<Omit<Omit<EndpointWithDataConfig, "requestSchema"> & { | ||
requestSchema?: AnyZodObject | undefined; | ||
}, "method">>) => (request?: { | ||
headers?: Record<string, string> | undefined; | ||
baseURL?: string | undefined; | ||
validateStatus?: ((status: number) => boolean) | undefined; | ||
body?: BodyInit | null | undefined; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method?: string | undefined; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
} | undefined) => Promise<any>; | ||
head: (options: NoInfer<Omit<EndpointConfig, "method">>) => (request?: { | ||
headers?: Record<string, string> | undefined; | ||
baseURL?: string | undefined; | ||
validateStatus?: ((status: number) => boolean) | undefined; | ||
body?: BodyInit | null | undefined; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method?: string | undefined; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
} | undefined) => Promise<any>; | ||
options: (options: NoInfer<Omit<EndpointConfig, "method">>) => (request?: { | ||
headers?: Record<string, string> | undefined; | ||
baseURL?: string | undefined; | ||
validateStatus?: ((status: number) => boolean) | undefined; | ||
body?: BodyInit | null | undefined; | ||
cache?: RequestCache | undefined; | ||
credentials?: RequestCredentials | undefined; | ||
integrity?: string | undefined; | ||
keepalive?: boolean | undefined; | ||
method?: string | undefined; | ||
mode?: RequestMode | undefined; | ||
priority?: RequestPriority | undefined; | ||
redirect?: RequestRedirect | undefined; | ||
referrer?: string | undefined; | ||
referrerPolicy?: ReferrerPolicy | undefined; | ||
signal?: AbortSignal | null | undefined; | ||
window?: null | undefined; | ||
} | undefined) => Promise<any>; | ||
declareEndpoint: <Config extends EndpointConfig | EndpointWithDataConfig>(options: Config) => Util_FlatType<Endpoint<Config>>; | ||
declareEndpoint: <Config extends EndpointConfig | EndpointWithDataConfig>(options: Config) => Util_FlatType<Endpoint<Config>> & { | ||
config: Config; | ||
}; | ||
declareBlobEndpoint: <Config_1 extends BlobEndpointConfig>(options: Config_1) => Util_FlatType<BlobRequestEndpoint<Config_1>>; | ||
@@ -383,0 +122,0 @@ provideClient: (newClient: Navios) => void; |
@@ -37,6 +37,9 @@ "use strict"; | ||
return Array.from(match).map(([, group]) => group).reduce( | ||
(newMessage, param) => newMessage.replaceAll( | ||
new RegExp(`\\$${param}`, "g"), | ||
(newMessage, param) => ( | ||
// @ts-expect-error TS18048 we checked urlParams before | ||
params.urlParams[param] | ||
params.urlParams[param] ? newMessage.replaceAll( | ||
new RegExp(`\\$${param}`, "g"), | ||
// @ts-expect-error TS18048 we checked urlParams before | ||
params.urlParams[param] | ||
) : newMessage | ||
), | ||
@@ -61,6 +64,6 @@ urlPart | ||
// packages/navios-zod/src/createAPI.mts | ||
var import_navios2 = require("navios"); | ||
var import_navios3 = require("navios"); | ||
// packages/navios-zod/src/declareAPI.mts | ||
var import_navios = require("navios"); | ||
var import_navios2 = require("navios"); | ||
@@ -74,2 +77,57 @@ // packages/navios-zod/src/NaviosZodError.mts | ||
// packages/navios-zod/src/utils/handleException.mts | ||
var import_navios = require("navios"); | ||
var import_zod = require("zod"); | ||
function handleException(config, error, responseSchema) { | ||
if (!config.useDiscriminatorResponse) { | ||
throw error; | ||
} | ||
if (error instanceof import_navios.NaviosError && error.response) { | ||
if (config.useWholeResponse) { | ||
return responseSchema.parse(error.response); | ||
} | ||
return responseSchema.parse(error.response.data); | ||
} | ||
throw error; | ||
} | ||
// packages/navios-zod/src/utils/makeRequestConfig.mts | ||
function makeRequestConfig(request, options, method, finalUrlPart) { | ||
return { | ||
...request, | ||
params: options.querySchema ? ( | ||
// @ts-expect-error TS2339 We know that sometimes querySchema can generate a default value | ||
options.querySchema.parse(request.params) | ||
) : {}, | ||
method, | ||
url: finalUrlPart, | ||
data: "requestSchema" in options ? ( | ||
// @ts-expect-error TS2339 We know that sometimes querySchema can generate a default value | ||
options.requestSchema.parse(request.data) | ||
) : void 0 | ||
}; | ||
} | ||
// packages/navios-zod/src/utils/endpointCreator.mts | ||
function endpointCreator(options, { getClient, config }) { | ||
const { method, url, responseSchema } = options; | ||
const handler = async (request = {}) => { | ||
const client = getClient(); | ||
const finalUrlPart = bindUrlParams(url, request); | ||
try { | ||
const result = await client.request( | ||
makeRequestConfig(request, options, method, finalUrlPart) | ||
); | ||
if (config.useWholeResponse) { | ||
return responseSchema.parse(result); | ||
} | ||
return responseSchema.parse(result.data); | ||
} catch (error) { | ||
return handleException(config, error, responseSchema); | ||
} | ||
}; | ||
handler.config = options; | ||
return handler; | ||
} | ||
// packages/navios-zod/src/declareAPI.mts | ||
@@ -87,39 +145,2 @@ function declareAPI({ | ||
} | ||
function declareEndpoint(options) { | ||
const { method, url, responseSchema } = options; | ||
return async (request = {}) => { | ||
const client2 = getClient(); | ||
const finalUrlPart = bindUrlParams(url, request); | ||
try { | ||
const result = await client2.request({ | ||
...request, | ||
params: options.querySchema ? ( | ||
// @ts-expect-error TS2339 We know that sometimes querySchema can generate a default value | ||
options.querySchema.parse(request.params) | ||
) : {}, | ||
method, | ||
url: finalUrlPart, | ||
data: "requestSchema" in options ? ( | ||
// @ts-expect-error TS2339 We know that sometimes querySchema can generate a default value | ||
options.requestSchema.parse(request.data) | ||
) : void 0 | ||
}); | ||
if (useWholeResponse) { | ||
return responseSchema.parse(result); | ||
} | ||
return responseSchema.parse(result.data); | ||
} catch (error) { | ||
if (!useDiscriminatorResponse) { | ||
throw error; | ||
} | ||
if (error instanceof import_navios.NaviosError && error.response) { | ||
if (useWholeResponse) { | ||
return responseSchema.parse(error.response); | ||
} | ||
return responseSchema.parse(error.response.data); | ||
} | ||
throw error; | ||
} | ||
}; | ||
} | ||
function declareBlobEndpoint(options) { | ||
@@ -154,9 +175,7 @@ const { method, url, download } = options; | ||
} | ||
function makeMethodCreator(method) { | ||
return (options) => { | ||
return declareEndpoint({ | ||
method, | ||
...options | ||
}); | ||
}; | ||
function declareEndpoint(options) { | ||
return endpointCreator(options, { | ||
getClient, | ||
config: { useDiscriminatorResponse, useWholeResponse } | ||
}); | ||
} | ||
@@ -167,9 +186,2 @@ function provideClient(newClient) { | ||
return { | ||
get: makeMethodCreator("GET"), | ||
post: makeMethodCreator("POST"), | ||
put: makeMethodCreator("PUT"), | ||
delete: makeMethodCreator("DELETE"), | ||
patch: makeMethodCreator("PATCH"), | ||
head: makeMethodCreator("HEAD"), | ||
options: makeMethodCreator("OPTIONS"), | ||
declareEndpoint, | ||
@@ -184,3 +196,3 @@ declareBlobEndpoint, | ||
function createAPI(config) { | ||
const client = (0, import_navios2.create)({ | ||
const client = (0, import_navios3.create)({ | ||
headers: { | ||
@@ -187,0 +199,0 @@ "Content-Type": "application/json", |
{ | ||
"name": "@navios/navios-zod", | ||
"version": "0.4.0", | ||
"version": "0.5.0-alpha.0", | ||
"author": { | ||
@@ -8,2 +8,7 @@ "name": "Oleksandr Hanzha", | ||
}, | ||
"repository": { | ||
"directory": "packages/navios-zod", | ||
"type": "git", | ||
"url": "https://github.com/Arilas/navios.git" | ||
}, | ||
"license": "MIT", | ||
@@ -14,3 +19,3 @@ "typings": "./dist/index.d.ts", | ||
"peerDependencies": { | ||
"navios": "0.4.0", | ||
"navios": "^0.4.0", | ||
"zod": "^3.23.8" | ||
@@ -17,0 +22,0 @@ }, |
@@ -38,4 +38,5 @@ # Navios Zod API | ||
const getUser = API.get({ | ||
user: 'user', | ||
const getUser = API.declareEndpoint({ | ||
method: 'get', | ||
url: 'user', | ||
responseSchema: GetUserResponseSchema, | ||
@@ -75,3 +76,4 @@ }) | ||
const updateUser = API.put({ | ||
const updateUser = API.declareEndpoint({ | ||
method: 'PUT', | ||
url: 'user/$userId', | ||
@@ -141,12 +143,3 @@ requestSchema: UpdateUserRequestSchema, | ||
#### `get`, `post`, `put`, `delete`, `patch`, `options` - creates an endpoint with the specified options. | ||
```ts | ||
API.get({ | ||
url: string, | ||
responseSchema: z.ZodSchema<unknown>, | ||
querySchema?: z.ZodSchema<unknown>, | ||
}) | ||
``` | ||
### `createAPI` | ||
@@ -153,0 +146,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
20
0
69899
492
155