Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

http-kit

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-kit - npm Package Compare versions

Comparing version
0.0.3
to
0.1.0-beta
+2
dist/request/index.d.ts
export * from "./mod.js";
export * from "./Request.js";
export * from "./mod.js";
export * from "./Request.js";
import { RequestEffect } from "../types.js";
import { Request, RequestInit_ } from "./Request.js";
export declare function request(input: Request | string | URL, init?: RequestInit_ | undefined): RequestEffect;
export declare const get: (input: string | URL, init?: Omit<RequestInit_, "method"> | undefined) => RequestEffect;
export declare const post: (input: string | URL, body: RequestInit_["body"], init?: Omit<RequestInit_, "method" | "body"> | undefined) => RequestEffect;
export declare const put: (input: string | URL, body: RequestInit_["body"], init?: Omit<RequestInit_, "method" | "body"> | undefined) => RequestEffect;
export declare const head: (input: string | URL, init?: Omit<RequestInit_, "method"> | undefined) => RequestEffect;
export declare const options: (input: string | URL, init?: Omit<RequestInit_, "method"> | undefined) => RequestEffect;
export declare const patch: (input: string | URL, init?: Omit<RequestInit_, "method"> | undefined) => RequestEffect;
declare const delete_: (input: string | URL, init?: Omit<RequestInit_, "method"> | undefined) => RequestEffect;
export { delete_ as delete };
import * as Effect from "@effect/io/Effect";
import { isBody } from "../body/util.js";
import { Interpreter } from "../interpreter.js";
import { Request } from "./Request.js";
export function request(input, init = {}) {
return Effect.flatMap(Interpreter, (interpreter) => {
const url = typeof input === "string"
? interpreter.newURL(input)
: input instanceof Request
? interpreter.newURL(input.url)
: input;
let init_ = input instanceof Request ? input.init : init;
if (init_) {
if (init_.search)
url.search = init_.search;
if (isBody(init_.body)) {
const body = init_.body;
const headers = interpreter.newHeaders(init_.headers);
for (const key in body.headers) {
if (Object.prototype.hasOwnProperty.call(body.headers, key)) {
if (!headers.has(key))
headers.set(key, body.headers[key]);
}
}
init_.headers = headers;
init_.body = body.value;
}
}
return interpreter.execute(new Request(url, init_));
});
}
function make(method) {
return (input, init = {}) => request(input, Object.assign(Object.assign({}, init), { method }));
}
function make2(method) {
return (input, body, init = {}) => request(input, Object.assign(Object.assign({}, init), { body, method }));
}
export const get = make("GET");
export const post = make2("POST");
export const put = make2("PUT");
export const head = make("HEAD");
export const options = make("OPTIONS");
export const patch = make("PATCH");
const delete_ = make("DELETE");
export { delete_ as delete };
import { Body as RequestBody } from "../body/types.js";
export type RequestInit_ = Omit<RequestInit, "body" | "signal"> & {
search?: string;
body?: BodyInit | RequestBody;
};
export declare class Request {
readonly _tag = "HttpRequest";
readonly url: string;
readonly init: RequestInit_ & {
method: string;
};
constructor(input: string | URL, init?: RequestInit_);
clone(): Request;
}
export class Request {
constructor(input, init) {
var _a;
this._tag = "HttpRequest";
this.url = typeof input === "string" ? input : input.toString();
this.init = Object.assign(Object.assign({}, init), { method: (_a = init === null || init === void 0 ? void 0 : init.method) !== null && _a !== void 0 ? _a : "GET" });
}
clone() {
return new Request(this.url, this.init ? Object.assign({}, this.init) : undefined);
}
}
+2
-1
import * as E from "@effect/io/Effect";
import { Interpreter } from "../interpreter.js";
import { Req } from "../types.js";
import { Interceptor } from "./types.js";
export declare function execute(request: Request, interceptors?: Array<Interceptor>): E.Effect<Interpreter, import("../exception.js").Err, Response>;
export declare function execute(request: Req, interceptors?: Array<Interceptor>): E.Effect<Interpreter, import("../exception.js").Err, Response>;

@@ -17,10 +17,10 @@ import * as E from "@effect/io/Effect";

yield* s(E.logInfo(`Request interceptor exited: ${name}`));
if (interpreter.isRequest(result)) {
mutable_request = result;
if (interpreter.isResponse(result)) {
request_response = result;
yield s(E.logDebug(`Returning early from interceptor: ${name}`));
break;
}
else {
mutable_request = result;
request_response = result;
yield s(E.logDebug(`Returning early from interceptor: ${name}`));
break;
}

@@ -27,0 +27,0 @@ }

@@ -5,7 +5,8 @@ import * as Effect from "@effect/io/Effect";

import { Err } from "../exception.js";
import { Request } from "../request/Request.js";
export interface RequestInterceptor {
(req: Req): Effect.Effect<Interpreter, Err, Req | Res>;
(req: Request): Effect.Effect<Interpreter, Err, Req | Res>;
}
export interface ResponseInterceptor {
(res: Res, req: Req): Effect.Effect<Interpreter, Err, Res>;
(res: Res, req: Request): Effect.Effect<Interpreter, Err, Res>;
}

@@ -12,0 +13,0 @@ export type Interceptor = {

@@ -6,4 +6,4 @@ import * as Effect from "@effect/io/Effect";

import { ApplicationError, HttpError } from "../exception.js";
const fetch_ = (input, init) => {
return Effect.tryCatchPromiseInterrupt((signal) => fetch(input, Object.assign(Object.assign({}, init), { signal })), (error) => error instanceof Error
const fetch_ = (req) => {
return Effect.tryCatchPromiseInterrupt((signal) => fetch(req.url, Object.assign(Object.assign({}, req.init), { signal })), (error) => error instanceof Error
? error.name === "NetworkError"

@@ -14,9 +14,14 @@ ? new HttpError("Network error")

};
const createHeader = function (headers) {
const newHeaders = function (headers) {
return new Headers(headers);
};
// @ts-expect-error
const isRequest = function (request) {
return request instanceof Request;
const newURL = function (url) {
return new URL(url);
};
// const newRequest: Interpreter["newRequest"] = function (input, init) {
// return new Request(input, init);
// };
// const isRequest: Interpreter["isRequest"] = function (request) {
// return request instanceof Request;
// };
// @ts-expect-error

@@ -26,17 +31,15 @@ const isResponse = function (response) {

};
const service = {
newURL,
// isRequest,
isResponse,
newHeaders,
// newRequest,
execute: fetch_,
};
export function provide(effect, interceptors) {
return pipe(effect, Effect.provideService(Interpreter, Interpreter.of({
isRequest,
isResponse,
createHeader,
execute(input, init) {
return pipe(executor(new Request(input, init), interceptors), Effect.provideService(Interpreter, {
isRequest,
isResponse,
createHeader,
execute: fetch_,
}));
},
})));
return pipe(effect, Effect.provideService(Interpreter, Interpreter.of(Object.assign(Object.assign({}, service), { execute(request) {
return pipe(executor(request, interceptors), Effect.provideService(Interpreter, service));
} }))));
}
export const execute = flow(provide, Effect.runPromiseEither);

@@ -1,6 +0,6 @@

export * from "./request.js";
export * from "./function.js";
export * from "./exception.js";
export * from "./execute/mod.js";
export * from "./request/mod.js";
export type * from "./types.js";
export type * from "./execute/types.js";

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

export * from "./request.js";
export * from "./function.js";
export * from "./exception.js";
export * from "./execute/mod.js";
export * from "./request/mod.js";
import * as Ctx from "@effect/data/Context";
import * as Effect from "@effect/io/Effect";
import { Res } from "./types.js";
import { Res, Req } from "./types.js";
import { Err } from "./exception.js";
export type Executor = (input: RequestInfo | URL, init?: RequestInit | undefined) => Effect.Effect<never, Err, Res>;
export type Executor = (request: Req) => Effect.Effect<never, Err, Res>;
export interface Interpreter {
execute: Executor;
isRequest(request: unknown): request is Request;
newURL: (url: string) => URL;
isResponse: (response: unknown) => response is Response;
createHeader: (headers?: RequestInit["headers"]) => Headers;
newHeaders: (headers?: RequestInit["headers"]) => Headers;
}
export declare const Interpreter: Ctx.Tag<Interpreter, Interpreter>;
import type * as Effect from "@effect/io/Effect";
import { Interpreter } from "./interpreter.js";
import { Err } from "./exception.js";
import { Request } from "./request/Request.js";
export type Req = Request;

@@ -5,0 +6,0 @@ export type Res = Response;

{
"name": "http-kit",
"version": "0.0.3",
"version": "0.1.0-beta",
"type": "module",

@@ -52,4 +52,4 @@ "description": "Platform agnostic fetch kit for the Effect package",

"./request": {
"import": "./dist/request.js",
"types": "./dist/request.d.ts"
"import": "./dist/request/index.js",
"types": "./dist/request/index.d.ts"
},

@@ -56,0 +56,0 @@ "./interpreter": {

import { Body as ReqBody } from "./body/types.js";
import { RequestEffect } from "./types.js";
type RequestInput = string | URL;
export type ReqInit = Omit<RequestInit, "body"> & {
search?: string;
body?: BodyInit | ReqBody;
};
export declare function request(input: RequestInput, { body, ...init }?: ReqInit | undefined): RequestEffect;
export declare const get: (input: RequestInput, init?: Omit<ReqInit, "method"> | undefined) => RequestEffect;
export declare const post: (input: RequestInput, body: ReqInit["body"], init?: Omit<ReqInit, "method" | "body"> | undefined) => RequestEffect;
export declare const put: (input: RequestInput, body: ReqInit["body"], init?: Omit<ReqInit, "method" | "body"> | undefined) => RequestEffect;
export declare const head: (input: RequestInput, init?: Omit<ReqInit, "method"> | undefined) => RequestEffect;
export declare const options: (input: RequestInput, init?: Omit<ReqInit, "method"> | undefined) => RequestEffect;
export declare const patch: (input: RequestInput, init?: Omit<ReqInit, "method"> | undefined) => RequestEffect;
declare const delete_: (input: RequestInput, init?: Omit<ReqInit, "method"> | undefined) => RequestEffect;
export { delete_ as delete };
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import * as Effect from "@effect/io/Effect";
import { isBody } from "./body/util.js";
import { Interpreter } from "./interpreter.js";
export function request(input, _a = {}) {
var { body } = _a, init = __rest(_a, ["body"]);
return Effect.flatMap(Interpreter, (interpreter) => {
const url = typeof input === "string" ? new URL(input) : input;
if (init === null || init === void 0 ? void 0 : init.search)
url.search = init.search;
let new_init = Object.assign({}, init);
if (isBody(body)) {
const headers = interpreter.createHeader(new_init.headers);
for (const key in body === null || body === void 0 ? void 0 : body.headers) {
if (Object.prototype.hasOwnProperty.call(body.headers, key)) {
if (!headers.has(key))
headers.set(key, body.headers[key]);
}
}
new_init.headers = headers;
new_init.body = body.value;
}
return interpreter.execute(url, new_init);
});
}
function make(method) {
return (input, init = {}) => request(input, Object.assign(Object.assign({}, init), { method }));
}
function make2(method) {
return (input, body, init = {}) => request(input, Object.assign(Object.assign({}, init), { body, method }));
}
export const get = make("GET");
export const post = make2("POST");
export const put = make2("PUT");
export const head = make("HEAD");
export const options = make("OPTIONS");
export const patch = make("PATCH");
const delete_ = make("DELETE");
export { delete_ as delete };