Skip to content

Commit

Permalink
feat: add lintstaged
Browse files Browse the repository at this point in the history
  • Loading branch information
cesconix committed Jun 26, 2024
1 parent b39b9dd commit 2e254e2
Show file tree
Hide file tree
Showing 20 changed files with 541 additions and 230 deletions.
3 changes: 1 addition & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

pnpm biome
pnpm test
npx lint-staged && pnpm test
3 changes: 3 additions & 0 deletions lint-staged.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
"*": ["pnpm biome", "pnpm test"]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"clean": "turbo clean && rimraf node_modules .turbo",
"build": "turbo build",
"dev": "turbo dev",
"biome": "biome check --write --no-errors-on-unmatched",
"biome": "biome check --no-errors-on-unmatched",
"changeset": "changeset",
"changeset:version": "changeset version",
"changeset:release": "changeset publish",
Expand All @@ -26,6 +26,7 @@
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"husky": "^9.0.11",
"lint-staged": "^15.2.7",
"rimraf": "^5.0.7",
"turbo": "^2.0.3"
},
Expand Down
4 changes: 1 addition & 3 deletions packages/pinorama-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
}
},
"types": "./dist/types/pinorama-client.d.ts",
"files": [
"dist"
],
"files": ["dist"],
"scripts": {
"clean": "rimraf dist node_modules",
"build": "rollup --config"
Expand Down
46 changes: 23 additions & 23 deletions packages/pinorama-client/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import alias from "@rollup/plugin-alias";
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import terser from "@rollup/plugin-terser";
import typescript from "@rollup/plugin-typescript";
import { dts } from "rollup-plugin-dts";
import alias from "@rollup/plugin-alias"
import commonjs from "@rollup/plugin-commonjs"
import resolve from "@rollup/plugin-node-resolve"
import terser from "@rollup/plugin-terser"
import typescript from "@rollup/plugin-typescript"
import { dts } from "rollup-plugin-dts"

const inputFile = "src/index.ts";
const outputFileName = "pinorama-client";
const inputFile = "src/index.ts"
const outputFileName = "pinorama-client"

export default [
// Declaration file
{
input: inputFile,
output: [{ file: "dist/types/pinorama-client.d.ts", format: "es" }],
plugins: [dts()],
plugins: [dts()]
},

// Browser ESM build
Expand All @@ -23,21 +23,21 @@ export default [
{
file: `dist/browser/${outputFileName}.esm.js`,
format: "es",
sourcemap: true,
},
sourcemap: true
}
],
plugins: [
alias({
entries: [
{ find: "./platform/node.js", replacement: "./platform/browser.js" },
],
{ find: "./platform/node.js", replacement: "./platform/browser.js" }
]
}),
resolve({ browser: true }),
commonjs(),
typescript(),
terser(),
terser()
],
external: ["zod"],
external: ["zod"]
},

// // Browser UMD build
Expand Down Expand Up @@ -92,11 +92,11 @@ export default [
{
file: `dist/node/${outputFileName}.mjs`,
format: "es",
sourcemap: true,
},
sourcemap: true
}
],
plugins: [resolve(), commonjs(), typescript()],
external: ["zod"],
external: ["zod"]
},

// Node.js CJS build
Expand All @@ -106,10 +106,10 @@ export default [
{
file: `dist/node/${outputFileName}.cjs`,
format: "cjs",
sourcemap: true,
},
sourcemap: true
}
],
plugins: [resolve(), commonjs(), typescript()],
external: ["zod"],
},
];
external: ["zod"]
}
]
90 changes: 45 additions & 45 deletions packages/pinorama-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
import { z } from "zod";
import { setTimeout } from "./platform/node.js";
import { z } from "zod"
import { setTimeout } from "./platform/node.js"

const clientOptionsSchema = z.object({
url: z.string(),
maxRetries: z.number().min(2),
backoff: z.number().min(0),
backoffFactor: z.number().min(2),
backoffMax: z.number().min(1000),
adminSecret: z.string().optional(),
});
adminSecret: z.string().optional()
})

export type PinoramaClientOptions = z.infer<typeof clientOptionsSchema>;
export type PinoramaClientOptions = z.infer<typeof clientOptionsSchema>

export const defaultClientOptions: Partial<PinoramaClientOptions> = {
maxRetries: 5,
backoff: 1000,
backoffFactor: 2,
backoffMax: 30000,
};
backoffMax: 30000
}

export class PinoramaClient {
/** Pinorama Server URL */
private url: string;
private url: string

/** Maximum number of retry attempts */
private maxRetries: number;
private maxRetries: number

/** Initial backoff delay in milliseconds */
private backoff: number;
private backoff: number

/** Factor by which the backoff delay increases */
private backoffFactor: number;
private backoffFactor: number

/** Maximum backoff delay in milliseconds */
private backoffMax: number;
private backoffMax: number

/** Default headers for HTTP requests */
private defaultHeaders: Record<string, string>;
private defaultHeaders: Record<string, string>

constructor(options?: Partial<PinoramaClientOptions>) {
const opts = clientOptionsSchema.parse({
...defaultClientOptions,
...options,
});
...options
})

this.url = opts.url;
this.maxRetries = opts.maxRetries;
this.backoff = opts.backoff;
this.backoffFactor = opts.backoffFactor;
this.backoffMax = opts.backoffMax;
this.url = opts.url
this.maxRetries = opts.maxRetries
this.backoff = opts.backoff
this.backoffFactor = opts.backoffFactor
this.backoffMax = opts.backoffMax

this.defaultHeaders = this.getDefaultHeaders(opts);
this.defaultHeaders = this.getDefaultHeaders(opts)
}

private getDefaultHeaders(options: Partial<PinoramaClientOptions>) {
const headers: Record<string, string> = {
"content-type": "application/json",
};
"content-type": "application/json"
}

if (options.adminSecret) {
headers["x-pinorama-admin-secret"] = options.adminSecret;
headers["x-pinorama-admin-secret"] = options.adminSecret
}

return headers;
return headers
}

private async retryOperation(operation: () => Promise<void>): Promise<void> {
let retries = 0;
let currentBackoff = this.backoff;
let retries = 0
let currentBackoff = this.backoff
while (retries < this.maxRetries) {
try {
await operation();
return;
await operation()
return
} catch (error) {
console.error("error during operation:", error);
retries++;
if (retries >= this.maxRetries) throw new Error("max retries reached");
await setTimeout(Math.min(currentBackoff, this.backoffMax));
currentBackoff *= this.backoffFactor;
console.error("error during operation:", error)
retries++
if (retries >= this.maxRetries) throw new Error("max retries reached")
await setTimeout(Math.min(currentBackoff, this.backoffMax))
currentBackoff *= this.backoffFactor
}
}
}
Expand All @@ -87,32 +87,32 @@ export class PinoramaClient {
const response = await fetch(`${this.url}/bulk`, {
method: "POST",
headers: this.defaultHeaders,
body: JSON.stringify(docs),
});
body: JSON.stringify(docs)
})

if (response.status !== 201) {
throw new Error("[TODO ERROR]: PinoramaClient.insert failed");
throw new Error("[TODO ERROR]: PinoramaClient.insert failed")
}
});
})
}

public async search(payload: unknown): Promise<unknown> {
try {
const response = await fetch(`${this.url}/search`, {
method: "POST",
headers: this.defaultHeaders,
body: JSON.stringify(payload),
});
body: JSON.stringify(payload)
})

const json = await response.json();
const json = await response.json()
if (response.status !== 200) {
throw new Error(json.error);
throw new Error(json.error)
}

return json;
return json
} catch (error) {
console.error("error searching logs:", error);
throw error;
console.error("error searching logs:", error)
throw error
}
}
}
4 changes: 2 additions & 2 deletions packages/pinorama-client/src/platform/browser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const setTimeout = (ms: number) =>
new Promise((resolve) => window.setTimeout(resolve, ms));
new Promise((resolve) => window.setTimeout(resolve, ms))

export { setTimeout };
export { setTimeout }
4 changes: 2 additions & 2 deletions packages/pinorama-client/src/platform/node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { setTimeout } from "node:timers/promises";
import { setTimeout } from "node:timers/promises"

export { setTimeout };
export { setTimeout }
15 changes: 3 additions & 12 deletions packages/pinorama-client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,8 @@
"moduleResolution": "Node",
"module": "ESNext",
"sourceMap": true,
"lib": [
"ESNext",
"DOM"
],
"lib": ["ESNext", "DOM"]
},
"include": [
"src"
],
"exclude": [
"node_modules",
"dist",
"tests"
]
"include": ["src"],
"exclude": ["node_modules", "dist", "tests"]
}
4 changes: 1 addition & 3 deletions packages/pinorama-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"type": "module",
"types": "./dist/index.d.mts",
"exports": "./dist/index.mjs",
"files": [
"dist"
],
"files": ["dist"],
"scripts": {
"clean": "rimraf dist node_modules",
"build": "tsc"
Expand Down
Loading

0 comments on commit 2e254e2

Please sign in to comment.