@vyriy/request 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Vyriy contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # @vyriy/request
2
+
3
+ Shared request utility for Vyriy projects.
4
+
5
+ ## Purpose
6
+
7
+ This package provides a small wrapper around `fetch` with timeout handling, retry behavior for retryable failures, and basic response parsing for JSON and text payloads.
8
+
9
+ ## Install
10
+
11
+ With npm:
12
+
13
+ ```bash
14
+ npm install @vyriy/request
15
+ ```
16
+
17
+ With Yarn:
18
+
19
+ ```bash
20
+ yarn add @vyriy/request
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ```ts
26
+ import { request } from '@vyriy/request';
27
+
28
+ const profile = await request<{ id: string; name: string }>('/api/profile');
29
+
30
+ const html = await request<string>('https://example.com', null, {
31
+ retries: 1,
32
+ timeout: 5000,
33
+ });
34
+ ```
35
+
36
+ ## API
37
+
38
+ - `request(input, init?, options?)` performs a `fetch` call and returns parsed JSON or text.
39
+ - `init` may be `null` when you want to skip request init and pass `options` as the third argument.
40
+ - `options.timeout` sets the per-attempt timeout in milliseconds.
41
+ - `options.retries` controls how many retry attempts are allowed after the initial request.
42
+ - `options.retryDelay` sets the base delay in milliseconds between retry attempts.
43
+ - `options.retryMethods` overrides which HTTP methods are allowed to retry.
44
+ - `options.retryStatuses` overrides which HTTP status codes are considered retryable.
package/constants.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare const DEFAULT_RETRY_METHODS: string[];
2
+ export declare const DEFAULT_RETRY_STATUSES: number[];
package/constants.js ADDED
@@ -0,0 +1,15 @@
1
+ export const DEFAULT_RETRY_METHODS = [
2
+ 'GET',
3
+ 'HEAD',
4
+ 'OPTIONS',
5
+ 'DELETE',
6
+ ];
7
+ export const DEFAULT_RETRY_STATUSES = [
8
+ 408,
9
+ 425,
10
+ 429,
11
+ 500,
12
+ 502,
13
+ 503,
14
+ 504,
15
+ ];
package/error.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export declare class HttpError extends Error {
2
+ readonly status: number;
3
+ readonly url: string;
4
+ constructor(response: Response);
5
+ }
6
+ export declare class TimeoutError extends Error {
7
+ readonly timeout: number;
8
+ constructor(timeout: number);
9
+ }
package/error.js ADDED
@@ -0,0 +1,19 @@
1
+ export class HttpError extends Error {
2
+ status;
3
+ url;
4
+ constructor(response) {
5
+ const details = response.statusText ? ` ${response.statusText}` : '';
6
+ super(`Request failed with status ${response.status}${details}`);
7
+ this.name = 'HttpError';
8
+ this.status = response.status;
9
+ this.url = response.url;
10
+ }
11
+ }
12
+ export class TimeoutError extends Error {
13
+ timeout;
14
+ constructor(timeout) {
15
+ super(`Request timed out after ${timeout}ms`);
16
+ this.name = 'TimeoutError';
17
+ this.timeout = timeout;
18
+ }
19
+ }
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './request.js';
2
+ export type * from './types.js';
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './request.js';
package/package.json ADDED
@@ -0,0 +1,86 @@
1
+ {
2
+ "name": "@vyriy/request",
3
+ "version": "0.1.9",
4
+ "description": "Shared request utility for Vyriy projects",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "license": "MIT",
8
+ "types": "./index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./index.d.ts",
12
+ "import": "./index.js",
13
+ "default": "./index.js"
14
+ },
15
+ "./constants": {
16
+ "types": "./constants.d.ts",
17
+ "import": "./constants.js",
18
+ "default": "./constants.js"
19
+ },
20
+ "./constants.js": {
21
+ "types": "./constants.d.ts",
22
+ "import": "./constants.js",
23
+ "default": "./constants.js"
24
+ },
25
+ "./error": {
26
+ "types": "./error.d.ts",
27
+ "import": "./error.js",
28
+ "default": "./error.js"
29
+ },
30
+ "./error.js": {
31
+ "types": "./error.d.ts",
32
+ "import": "./error.js",
33
+ "default": "./error.js"
34
+ },
35
+ "./index": {
36
+ "types": "./index.d.ts",
37
+ "import": "./index.js",
38
+ "default": "./index.js"
39
+ },
40
+ "./index.js": {
41
+ "types": "./index.d.ts",
42
+ "import": "./index.js",
43
+ "default": "./index.js"
44
+ },
45
+ "./request": {
46
+ "types": "./request.d.ts",
47
+ "import": "./request.js",
48
+ "default": "./request.js"
49
+ },
50
+ "./request.js": {
51
+ "types": "./request.d.ts",
52
+ "import": "./request.js",
53
+ "default": "./request.js"
54
+ },
55
+ "./response": {
56
+ "types": "./response.d.ts",
57
+ "import": "./response.js",
58
+ "default": "./response.js"
59
+ },
60
+ "./response.js": {
61
+ "types": "./response.d.ts",
62
+ "import": "./response.js",
63
+ "default": "./response.js"
64
+ },
65
+ "./retry": {
66
+ "types": "./retry.d.ts",
67
+ "import": "./retry.js",
68
+ "default": "./retry.js"
69
+ },
70
+ "./retry.js": {
71
+ "types": "./retry.d.ts",
72
+ "import": "./retry.js",
73
+ "default": "./retry.js"
74
+ },
75
+ "./timeout": {
76
+ "types": "./timeout.d.ts",
77
+ "import": "./timeout.js",
78
+ "default": "./timeout.js"
79
+ },
80
+ "./timeout.js": {
81
+ "types": "./timeout.d.ts",
82
+ "import": "./timeout.js",
83
+ "default": "./timeout.js"
84
+ }
85
+ }
86
+ }
package/request.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { Request } from './types.js';
2
+ export declare const request: Request;
package/request.js ADDED
@@ -0,0 +1,33 @@
1
+ import { DEFAULT_RETRY_METHODS, DEFAULT_RETRY_STATUSES } from './constants.js';
2
+ import { TimeoutError } from './error.js';
3
+ import { assertSuccessfulResponse, parseResponse } from './response.js';
4
+ import { isRetryableError, isRetryableMethod, wait } from './retry.js';
5
+ import { withTimeoutSignal } from './timeout.js';
6
+ export const request = async (input, init = {}, options = {}) => {
7
+ const normalizedInit = init ?? {};
8
+ const { retries = 2, retryDelay = 300, retryMethods = DEFAULT_RETRY_METHODS, retryStatuses = DEFAULT_RETRY_STATUSES, timeout = 25000, } = options;
9
+ const method = normalizedInit.method?.toUpperCase() ?? 'GET';
10
+ const run = async (attempt = 0) => {
11
+ const { cleanup, signal, timeoutSignal } = withTimeoutSignal(timeout, normalizedInit.signal);
12
+ try {
13
+ const response = await fetch(input, { ...normalizedInit, signal });
14
+ assertSuccessfulResponse(response);
15
+ const result = await parseResponse(response);
16
+ cleanup();
17
+ return result;
18
+ }
19
+ catch (error) {
20
+ const canRetry = attempt < retries &&
21
+ isRetryableMethod(method, retryMethods) &&
22
+ isRetryableError(error, normalizedInit.signal, retryStatuses);
23
+ if (!canRetry) {
24
+ cleanup();
25
+ throw timeoutSignal.aborted && timeoutSignal.reason instanceof TimeoutError ? timeoutSignal.reason : error;
26
+ }
27
+ await wait(retryDelay * (attempt + 1));
28
+ cleanup();
29
+ return run(attempt + 1);
30
+ }
31
+ };
32
+ return run();
33
+ };
package/response.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare const isJsonResponse: (response: Response) => boolean;
2
+ export declare const parseResponse: <R>(response: Response) => Promise<R>;
3
+ export declare const assertSuccessfulResponse: (response: Response) => void;
package/response.js ADDED
@@ -0,0 +1,18 @@
1
+ import { HttpError } from './error.js';
2
+ export const isJsonResponse = (response) => {
3
+ const contentType = response.headers.get('content-type');
4
+ return contentType?.includes('application/json') === true || contentType?.includes('+json') === true;
5
+ };
6
+ export const parseResponse = async (response) => {
7
+ if (isJsonResponse(response)) {
8
+ const result = await response.json();
9
+ return result;
10
+ }
11
+ const result = await response.text();
12
+ return result;
13
+ };
14
+ export const assertSuccessfulResponse = (response) => {
15
+ if (response.type !== 'opaque' && !response.ok) {
16
+ throw new HttpError(response);
17
+ }
18
+ };
package/retry.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export declare const isAbortError: (error: unknown) => boolean;
2
+ export declare const isRetryableMethod: (method: string, retryMethods: string[]) => boolean;
3
+ export declare const isRetryableError: (error: unknown, signal: AbortSignal | null | undefined, retryStatuses: number[]) => boolean;
4
+ export declare const wait: (delay: number) => Promise<void>;
package/retry.js ADDED
@@ -0,0 +1,23 @@
1
+ import { HttpError, TimeoutError } from './error.js';
2
+ export const isAbortError = (error) => error instanceof DOMException ? error.name === 'AbortError' : false;
3
+ export const isRetryableMethod = (method, retryMethods) => retryMethods.includes(method.toUpperCase());
4
+ export const isRetryableError = (error, signal, retryStatuses) => {
5
+ if (signal?.aborted) {
6
+ return false;
7
+ }
8
+ if (error instanceof TimeoutError) {
9
+ return true;
10
+ }
11
+ if (error instanceof HttpError) {
12
+ return retryStatuses.includes(error.status);
13
+ }
14
+ return !isAbortError(error);
15
+ };
16
+ export const wait = async (delay) => {
17
+ if (delay <= 0) {
18
+ return;
19
+ }
20
+ await new Promise((resolve) => {
21
+ setTimeout(resolve, delay);
22
+ });
23
+ };
package/timeout.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export declare const withTimeoutSignal: (timeout: number, signal?: AbortSignal | null) => {
2
+ cleanup: () => void;
3
+ signal: AbortSignal;
4
+ timeoutSignal: AbortSignal;
5
+ };
package/timeout.js ADDED
@@ -0,0 +1,24 @@
1
+ import { TimeoutError } from './error.js';
2
+ export const withTimeoutSignal = (timeout, signal) => {
3
+ const controller = new AbortController();
4
+ const timeoutId = setTimeout(() => controller.abort(new TimeoutError(timeout)), timeout);
5
+ const abortFromSource = () => {
6
+ controller.abort(signal?.reason);
7
+ };
8
+ if (signal) {
9
+ if (signal.aborted) {
10
+ abortFromSource();
11
+ }
12
+ else {
13
+ signal.addEventListener('abort', abortFromSource, { once: true });
14
+ }
15
+ }
16
+ return {
17
+ cleanup: () => {
18
+ clearTimeout(timeoutId);
19
+ signal?.removeEventListener('abort', abortFromSource);
20
+ },
21
+ signal: controller.signal,
22
+ timeoutSignal: controller.signal,
23
+ };
24
+ };
package/types.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export type Options = {
2
+ retries?: number;
3
+ timeout?: number;
4
+ retryDelay?: number;
5
+ retryMethods?: string[];
6
+ retryStatuses?: number[];
7
+ };
8
+ export type Request = <R = Record<string, unknown>>(input: RequestInfo | URL, init?: RequestInit | null, options?: Options) => Promise<R>;