@chronark/zod-bird
Advanced tools
Comparing version 0.3.4 to 0.3.5
@@ -67,26 +67,12 @@ "use strict"; | ||
} | ||
async fetch(pipe, parameters = {}, opts) { | ||
const url = new URL(`/v0/pipes/${pipe}.json`, this.baseUrl); | ||
for (const [key, value] of Object.entries(parameters)) { | ||
if (typeof value === "undefined" || value === null) { | ||
continue; | ||
} | ||
url.searchParams.set(key, value.toString()); | ||
} | ||
async fetch(url, opts) { | ||
for (let i = 0; i < 10; i++) { | ||
const res = await fetch(url, { | ||
headers: { | ||
Authorization: `Bearer ${this.token}` | ||
}, | ||
cache: opts?.cache, | ||
// @ts-ignore | ||
next: { | ||
revalidate: opts?.revalidate | ||
} | ||
}); | ||
const res = await fetch(url, opts); | ||
if (res.ok) { | ||
return res.json(); | ||
} | ||
if (res.status === 429) { | ||
await new Promise((r) => setTimeout(r, 1e3 + i ** 2 * 50)); | ||
if (res.status === 429 || res.status >= 500) { | ||
const delay = 1e3 + i ** 2 * 50; | ||
console.warn(`retrying ${url.toString()} in ${delay}ms`); | ||
await new Promise((r) => setTimeout(r, delay)); | ||
continue; | ||
@@ -114,3 +100,15 @@ } | ||
} | ||
const res = await this.fetch(req.pipe, validatedParams, req.opts); | ||
const url = new URL(`/v0/pipes/${req.pipe}.json`, this.baseUrl); | ||
if (validatedParams) { | ||
for (const [key, value] of Object.entries(validatedParams)) { | ||
if (typeof value === "undefined" || value === null) { | ||
continue; | ||
} | ||
url.searchParams.set(key, value.toString()); | ||
} | ||
} | ||
const res = await this.fetch(url, { | ||
method: "GET", | ||
headers: { Authorization: `Bearer ${this.token}` } | ||
}); | ||
const validatedResponse = outputSchema.safeParse(res); | ||
@@ -142,13 +140,10 @@ if (!validatedResponse.success) { | ||
const body = (Array.isArray(validatedEvents) ? validatedEvents : [validatedEvents]).map((p) => JSON.stringify(p)).join("\n"); | ||
const res = await fetch(url, { | ||
const res = await this.fetch(url, { | ||
method: "POST", | ||
body, | ||
headers: { Authorization: `Bearer ${this.token}` } | ||
headers: { Authorization: `Bearer ${this.token}`, "Content-Type": "application/json" } | ||
}).catch((err) => { | ||
throw new Error(`Unable to ingest to ${req.datasource}: ${err.message}`); | ||
}); | ||
if (!res.ok) { | ||
throw new Error( | ||
`Unable to ingest to ${req.datasource}: [${res.status}] ${await res.text()}` | ||
); | ||
} | ||
const validatedResponse = eventIngestReponseData.safeParse(await res.json()); | ||
const validatedResponse = eventIngestReponseData.safeParse(res); | ||
if (!validatedResponse.success) { | ||
@@ -155,0 +150,0 @@ throw new Error(validatedResponse.error.message); |
{ | ||
"name": "@chronark/zod-bird", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -34,25 +34,7 @@ import { z } from "zod"; | ||
private async fetch( | ||
pipe: string, | ||
parameters: Record<string, unknown> = {}, | ||
opts?: { cache?: RequestCache; revalidate?: number }, | ||
url: string | URL, | ||
opts: { method: string; headers?: Record<string, string>; body?: string }, | ||
): Promise<unknown> { | ||
const url = new URL(`/v0/pipes/${pipe}.json`, this.baseUrl); | ||
for (const [key, value] of Object.entries(parameters)) { | ||
if (typeof value === "undefined" || value === null) { | ||
continue; | ||
} | ||
url.searchParams.set(key, value.toString()); | ||
} | ||
for (let i = 0; i < 10; i++) { | ||
const res = await fetch(url, { | ||
headers: { | ||
Authorization: `Bearer ${this.token}`, | ||
}, | ||
cache: opts?.cache, | ||
// @ts-ignore | ||
next: { | ||
revalidate: opts?.revalidate, | ||
}, | ||
}); | ||
const res = await fetch(url, opts); | ||
if (res.ok) { | ||
@@ -62,7 +44,8 @@ return res.json(); | ||
if (res.status === 429) { | ||
await new Promise((r) => setTimeout(r, 1000 + i ** 2 * 50)); | ||
if (res.status === 429 || res.status >= 500) { | ||
const delay = 1000 + i ** 2 * 50; | ||
console.warn(`retrying ${url.toString()} in ${delay}ms`); | ||
await new Promise((r) => setTimeout(r, delay)); | ||
continue; | ||
} | ||
if (!res.ok) { | ||
@@ -102,3 +85,15 @@ const error = (await res.json()) as PipeErrorResponse; | ||
} | ||
const res = await this.fetch(req.pipe, validatedParams, req.opts); | ||
const url = new URL(`/v0/pipes/${req.pipe}.json`, this.baseUrl); | ||
if (validatedParams) { | ||
for (const [key, value] of Object.entries(validatedParams)) { | ||
if (typeof value === "undefined" || value === null) { | ||
continue; | ||
} | ||
url.searchParams.set(key, value.toString()); | ||
} | ||
} | ||
const res = await this.fetch(url, { | ||
method: "GET", | ||
headers: { Authorization: `Bearer ${this.token}` }, | ||
}); | ||
const validatedResponse = outputSchema.safeParse(res); | ||
@@ -144,16 +139,12 @@ if (!validatedResponse.success) { | ||
const res = await fetch(url, { | ||
const res = await this.fetch(url, { | ||
method: "POST", | ||
body, | ||
headers: { Authorization: `Bearer ${this.token}` }, | ||
headers: { Authorization: `Bearer ${this.token}`, "Content-Type": "application/json" }, | ||
}).catch((err) => { | ||
throw new Error(`Unable to ingest to ${req.datasource}: ${err.message}`); | ||
}); | ||
if (!res.ok) { | ||
throw new Error( | ||
`Unable to ingest to ${req.datasource}: [${res.status}] ${await res.text()}`, | ||
); | ||
} | ||
const validatedResponse = eventIngestReponseData.safeParse(res); | ||
const validatedResponse = eventIngestReponseData.safeParse(await res.json()); | ||
if (!validatedResponse.success) { | ||
@@ -160,0 +151,0 @@ throw new Error(validatedResponse.error.message); |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60210
2
804