You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

simple-typed-fetch

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-typed-fetch - npm Package Compare versions

Comparing version

to
0.1.6

dist/index.cjs

136

dist/index.d.ts

@@ -1,4 +0,132 @@

import "isomorphic-unfetch";
export { default as fetchWithValidation } from './fetchWithValidation';
export { default as simpleFetch } from './simpleFetch';
//# sourceMappingURL=index.d.ts.map
import * as neverthrow from 'neverthrow';
import { Schema, z } from 'zod';
declare function fetchWithValidation<DataOut, DataIn, ErrorOut, ErrorIn>(url: string, schema: Schema<DataOut, z.ZodTypeDef, DataIn>, options?: RequestInit, errorSchema?: Schema<ErrorOut, z.ZodTypeDef, ErrorIn>): Promise<neverthrow.Err<never, {
type: "fetchError";
url: string;
message: string;
error: Error;
}> | neverthrow.Err<never, {
type: "unknownFetchThrow";
url: string;
message: string;
error: unknown;
}> | neverthrow.Err<never, {
type: "unknownGetTextError";
url: string;
message: string;
error: Error;
}> | neverthrow.Err<never, {
type: "unknownGetTextUnknownError";
url: string;
message: string;
error: unknown;
}> | neverthrow.Err<never, {
type: "serverError";
url: string;
message: string;
status: number;
text: string;
}> | neverthrow.Ok<DataOut, never> | neverthrow.Err<never, {
type: "jsonParseError";
url: string;
message: string;
error: Error;
}> | neverthrow.Err<never, {
type: "jsonParseUnknownError";
url: string;
message: string;
error: unknown;
}> | neverthrow.Err<never, {
type: "clientErrorWithResponsePayload";
url: string;
message: string;
status: number;
payload: ErrorOut;
}> | neverthrow.Err<never, {
type: "clientErrorPayloadParseError";
url: string;
message: string;
status: number;
text: string;
error: z.ZodError<ErrorIn>;
}> | neverthrow.Err<never, {
type: "clientError";
url: string;
message: string;
status: number;
text: string;
}> | neverthrow.Err<never, {
type: "payloadParseError";
url: string;
message: string;
error: z.ZodError<DataIn>;
}>>;
declare class Wrapper<DataOut, DataIn, ErrorOut, ErrorIn> {
wrapped(url: string, schema: Schema<DataOut, z.ZodTypeDef, DataIn>, options?: RequestInit, errorSchema?: Schema<ErrorOut, z.ZodTypeDef, ErrorIn>): Promise<neverthrow.Err<never, {
type: "fetchError";
url: string;
message: string;
error: Error;
}> | neverthrow.Err<never, {
type: "unknownFetchThrow";
url: string;
message: string;
error: unknown;
}> | neverthrow.Err<never, {
type: "unknownGetTextError";
url: string;
message: string;
error: Error;
}> | neverthrow.Err<never, {
type: "unknownGetTextUnknownError";
url: string;
message: string;
error: unknown;
}> | neverthrow.Err<never, {
type: "serverError";
url: string;
message: string;
status: number;
text: string;
}> | neverthrow.Ok<DataOut, never> | neverthrow.Err<never, {
type: "jsonParseError";
url: string;
message: string;
error: Error;
}> | neverthrow.Err<never, {
type: "jsonParseUnknownError";
url: string;
message: string;
error: unknown;
}> | neverthrow.Err<never, {
type: "clientErrorWithResponsePayload";
url: string;
message: string;
status: number;
payload: ErrorOut;
}> | neverthrow.Err<never, {
type: "clientErrorPayloadParseError";
url: string;
message: string;
status: number;
text: string;
error: z.ZodError<ErrorIn>;
}> | neverthrow.Err<never, {
type: "clientError";
url: string;
message: string;
status: number;
text: string;
}> | neverthrow.Err<never, {
type: "payloadParseError";
url: string;
message: string;
error: z.ZodError<DataIn>;
}>>;
}
type FetchWithValidationInternalType<O, I, EO, EI> = ReturnType<Wrapper<O, I, EO, EI>['wrapped']>;
declare function simpleFetch<O, I, EO, EI, P extends unknown[]>(f: (...params: P) => FetchWithValidationInternalType<O, I, EO, EI>): (...params: Parameters<typeof f>) => Promise<O>;
export { fetchWithValidation, simpleFetch };

@@ -1,12 +0,146 @@

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
// src/index.ts
import "isomorphic-unfetch";
// src/fetchWithValidation.ts
import fetch from "isomorphic-unfetch";
import {
err,
fromPromise,
fromThrowable,
ok
} from "neverthrow";
async function fetchWithValidation(url, schema, options, errorSchema) {
const fetchResult = await fromPromise(fetch(url, {
...options ?? {},
headers: {
"Cache-Control": "no-store, max-age=0",
...options ? options.headers : {}
}
}), (e) => {
if (e instanceof Error) {
return err({
type: "fetchError",
url,
message: e.message,
error: e
});
}
return err({
type: "unknownFetchThrow",
url,
message: "Unknown fetch error",
error: e
});
});
if (fetchResult.isErr())
return fetchResult.error;
const response = fetchResult.value;
const textResult = await fromPromise(response.text(), (e) => {
if (e instanceof Error) {
return err({
type: "unknownGetTextError",
url,
message: `Can't get response content: ${e.message}`,
error: e
});
}
return err({
type: "unknownGetTextUnknownError",
url,
message: "Can't get response content: unknown error",
error: e
});
});
if (textResult.isErr())
return textResult.error;
const text = textResult.value;
if (response.status >= 500) {
return err({
type: "serverError",
url,
message: `Server error: ${response.status} ${response.statusText}`,
status: response.status,
text
});
}
const safeParseJson = fromThrowable(JSON.parse, (e) => {
if (e instanceof Error) {
return err({
type: "jsonParseError",
url,
message: e.message,
error: e
});
}
return err({
type: "jsonParseUnknownError",
url,
message: "Unknown JSON parse error",
error: e
});
});
const jsonResult = safeParseJson(text);
if (jsonResult.isErr()) {
const textPayload = schema.safeParse(text);
if (textPayload.success)
return ok(textPayload.data);
return jsonResult.error;
}
const json = jsonResult.value;
if (response.status >= 400) {
if (errorSchema) {
const serverError = errorSchema.safeParse(json);
if (serverError.success) {
return err({
type: "clientErrorWithResponsePayload",
url,
message: `Client error: ${response.status} ${response.statusText}. Server error: ${JSON.stringify(serverError.data)}`,
status: response.status,
payload: serverError.data
});
}
return err({
type: "clientErrorPayloadParseError",
url,
message: "Can't recognize error message. Response: " + text,
status: response.status,
text,
error: serverError.error
});
}
return err({
type: "clientError",
url,
message: `Error: ${response.status} ${response.statusText}. Response: ${text}`,
status: response.status,
text
});
}
const payload = schema.safeParse(json);
if (!payload.success) {
const issuesMessages = payload.error.issues.map((issue) => `[${issue.path.join(".")}] ${issue.message}`).join(", ");
return err({
type: "payloadParseError",
url,
message: `Can't recognize response payload: ${issuesMessages}`,
error: payload.error
});
}
return ok(payload.data);
}
// src/simpleFetch.ts
function simpleFetch(f) {
return async (...params) => {
const result = await f(...params);
if (result.isErr()) {
const { message, url } = result.error;
throw new Error(`${message} (${url})`);
}
return result.value;
};
}
export {
fetchWithValidation,
simpleFetch
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.simpleFetch = exports.fetchWithValidation = void 0;
require("isomorphic-unfetch");
var fetchWithValidation_1 = require("./fetchWithValidation");
Object.defineProperty(exports, "fetchWithValidation", { enumerable: true, get: function () { return __importDefault(fetchWithValidation_1).default; } });
var simpleFetch_1 = require("./simpleFetch");
Object.defineProperty(exports, "simpleFetch", { enumerable: true, get: function () { return __importDefault(simpleFetch_1).default; } });
//# sourceMappingURL=index.js.map

23

package.json

@@ -7,9 +7,21 @@ {

},
"version": "0.1.5",
"version": "0.1.6",
"description": "Making HTTP requests human way",
"main": "dist/index.js",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"type": "module",
"scripts": {
"prepack": "npm run build",
"build": "rimraf dist/ && tsc",
"clean": "rimraf dist/"
"clean": "rimraf dist/",
"build": "rimraf dist/ && tsup src/index.ts --format cjs,esm --dts --clean",
"watch": "npm run build -- --watch src",
"prepublishOnly": "npm run build"
},

@@ -35,4 +47,5 @@ "keywords": [

"devDependencies": {
"rimraf": "^4.4.1"
"rimraf": "^4.4.1",
"tsup": "^6.7.0"
}
}