@newstudios/http-utils
Advanced tools
Comparing version 1.0.3 to 1.0.4
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import { CookieSerializeOptions } from 'cookie'; | ||
import type { IncomingMessage, IncomingHttpHeaders, ServerResponse } from 'http'; | ||
import type { IncomingHttpHeaders as IncomingHttp2Headers } from 'http2'; | ||
import { HeadersInit } from 'node-fetch'; | ||
import { URL } from 'url'; | ||
import { Env } from './env'; | ||
@@ -24,3 +23,3 @@ declare const IP: unique symbol; | ||
} | ||
declare type HeaderType<F extends string> = Lowercase<F> extends keyof AllHeaders ? NonNullable<AllHeaders[Lowercase<F>]> : string | string[]; | ||
type HeaderType<F extends string> = string extends F ? string | string[] : Lowercase<F> extends keyof AllHeaders ? NonNullable<AllHeaders[Lowercase<F>]> : string | string[]; | ||
export { Env }; | ||
@@ -72,3 +71,3 @@ export declare function getHeader<F extends string>(req: IncomingMessage, field: F): HeaderType<F>; | ||
export declare function appendHeaders(res: ServerResponse, init: HeadersInit): void; | ||
export declare type SetCookieOptions = Omit<CookieSerializeOptions, 'encode'>; | ||
export type SetCookieOptions = Omit<CookieSerializeOptions, 'encode'>; | ||
/** | ||
@@ -75,0 +74,0 @@ * Get cookie from request headers cookie, value will be decoded with decodeURIComponent |
@@ -9,4 +9,3 @@ "use strict"; | ||
const utils_merge_1 = __importDefault(require("utils-merge")); | ||
const node_fetch_1 = require("node-fetch"); | ||
const url_1 = require("url"); | ||
const web_1 = require("./web"); | ||
const env_1 = require("./env"); | ||
@@ -73,3 +72,3 @@ Object.defineProperty(exports, "Env", { enumerable: true, get: function () { return env_1.Env; } }); | ||
try { | ||
req.memoizedURL = new url_1.URL(`${getOrigin(req)}${originalUrl}`); | ||
req.memoizedURL = new web_1.URL(`${getOrigin(req)}${originalUrl}`); | ||
} | ||
@@ -173,3 +172,3 @@ catch (err) { | ||
function appendHeaders(res, init) { | ||
const headers = new node_fetch_1.Headers(init); | ||
const headers = new web_1.Headers(init); | ||
headers.forEach((v, k) => appendHeader(res, k, v)); | ||
@@ -185,3 +184,3 @@ } | ||
if (!cookies) { | ||
cookies = cookie_1.parse(getHeader(req, 'cookie')); | ||
cookies = (0, cookie_1.parse)(getHeader(req, 'cookie')); | ||
req[COOKIE] = cookies; | ||
@@ -197,3 +196,3 @@ } | ||
function setCookie(res, name, val, options) { | ||
const opts = utils_merge_1.default({ path: '/' }, options); | ||
const opts = (0, utils_merge_1.default)({ path: '/' }, options); | ||
if (typeof opts.maxAge === 'number') { | ||
@@ -204,9 +203,9 @@ const maxAge = opts.maxAge || 0; | ||
} | ||
appendHeader(res, 'Set-Cookie', cookie_1.serialize(name, val, opts)); | ||
appendHeader(res, 'Set-Cookie', (0, cookie_1.serialize)(name, val, opts)); | ||
} | ||
exports.setCookie = setCookie; | ||
function clearCookie(res, name, options) { | ||
const opts = utils_merge_1.default({ expires: new Date(1), path: '/' }, options); | ||
const opts = (0, utils_merge_1.default)({ expires: new Date(1), path: '/' }, options); | ||
setCookie(res, name, '', opts); | ||
} | ||
exports.clearCookie = clearCookie; |
{ | ||
"name": "@newstudios/http-utils", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "http common lib", | ||
@@ -13,4 +13,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"cookie": "^0.4.1", | ||
"node-fetch": "^2.6.1", | ||
"cookie": "^0.5.0", | ||
"node-fetch": "^2", | ||
"url": "^0.11.0", | ||
@@ -20,9 +20,9 @@ "utils-merge": "^1.0.1" | ||
"devDependencies": { | ||
"@types/cookie": "^0.4.1", | ||
"@types/node": "^14.0.0", | ||
"@types/node-fetch": "^2.5.11", | ||
"@types/cookie": "^0.5.1", | ||
"@types/node": "^18.6.1", | ||
"@types/node-fetch": "^2", | ||
"@types/utils-merge": "^0.0.28", | ||
"rimraf": "^3.0.2", | ||
"tsc": "^2.0.3", | ||
"typescript": "^4.3.5" | ||
"typescript": "^4.9.5" | ||
}, | ||
@@ -36,3 +36,3 @@ "publishConfig": { | ||
], | ||
"author": "tangye@xinpianchang.com", | ||
"author": "447084+tangye1234@users.noreply.github.com", | ||
"homepage": "https://github.com/xinpianchang/xpc-http#readme", | ||
@@ -39,0 +39,0 @@ "repository": { |
@@ -6,4 +6,3 @@ import { CookieSerializeOptions, serialize, parse } from 'cookie' | ||
import merge from 'utils-merge' | ||
import { HeadersInit, Headers } from 'node-fetch' | ||
import { URL } from 'url' | ||
import { Headers, URL } from './web' | ||
import { Env } from './env' | ||
@@ -35,5 +34,7 @@ | ||
} | ||
type HeaderType<F extends string> = Lowercase<F> extends keyof AllHeaders | ||
? NonNullable<AllHeaders[Lowercase<F>]> | ||
: string | string[] | ||
type HeaderType<F extends string> = string extends F | ||
? string | string[] | ||
: Lowercase<F> extends keyof AllHeaders | ||
? NonNullable<AllHeaders[Lowercase<F>]> | ||
: string | string[] | ||
@@ -40,0 +41,0 @@ export { Env } |
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
25586
12
577
+ Addedcookie@0.5.0(transitive)
- Removedcookie@0.4.2(transitive)
Updatedcookie@^0.5.0
Updatednode-fetch@^2