flaks-request 1.0.7 → 1.0.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flaks-request",
3
- "version": "1.0.7",
3
+ "version": "1.0.10",
4
4
  "description": "Lightweight lib for requests",
5
5
  "keywords": [
6
6
  "flaks",
@@ -19,6 +19,7 @@
19
19
  },
20
20
  "files": [
21
21
  "src/**/*",
22
+ "types/**/*",
22
23
  "index.js",
23
24
  "index.d.ts"
24
25
  ]
@@ -120,10 +120,16 @@ function handleRequest(requestOptions, responseHandler) {
120
120
  const isHttps = url.startsWith("https://");
121
121
  parsed.port ??= (isHttps ? 443 : 80).toString();
122
122
 
123
+ if (requestOptions.search) {
124
+ const search = new URLSearchParams(requestOptions.search);
125
+ if (parsed.search.length) parsed.search += "&";
126
+
127
+ parsed.search += search.toString();
128
+ }
129
+
123
130
  /** @type {https.RequestOptions | http.RequestOptions} */
124
131
  const options = {
125
132
  ...requestOptions,
126
-
127
133
  protocol: parsed.protocol,
128
134
  hostname: parsed.hostname,
129
135
  path: parsed.pathname + parsed.search,
package/src/types.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  import { IncomingHttpHeaders, IncomingMessage } from "node:http";
2
2
 
3
- type SearchParamsValue = string | number | boolean | null | undefined | SearchParamsObject | unknown[];
3
+ /* type SearchParamsValue = string | number | boolean | null | undefined | SearchParamsObject | unknown[];
4
4
 
5
5
  export interface SearchParamsObject {
6
6
  [key: string]: SearchParamsValue;
7
- }
7
+ } */
8
8
 
9
9
  export interface RequestOptions {
10
10
  headers?: Record<string, string>;
11
- search?: SearchParamsObject;
11
+ search?: Record<string, string>;
12
12
  json?: unknown;
13
13
  method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD";
14
14
  path: string;
@@ -0,0 +1,9 @@
1
+ /** @import { RequestHandler, RequestOptions } from './types' */
2
+ export = handleRequest;
3
+ import type { RequestHandler, RequestOptions } from './types';
4
+ /**
5
+ *
6
+ * @param {RequestOptions} requestOptions
7
+ * @param {RequestHandler} responseHandler
8
+ */
9
+ declare function handleRequest(requestOptions: RequestOptions, responseHandler: RequestHandler): Promise<any>;
@@ -0,0 +1,47 @@
1
+ declare const _exports: {
2
+ buildSearchParams: typeof buildSearchParams;
3
+ safeParse: typeof safeParse;
4
+ resolveUrl: typeof resolveUrl;
5
+ buildResponse: typeof buildResponse;
6
+ };
7
+ export = _exports;
8
+ import type { JsonValue, SearchParamsObject } from './types';
9
+ import type { RequestOptions, RequestResponse } from './types';
10
+ import { IncomingMessage } from "node:http";
11
+ /**
12
+ *
13
+ * @param {SearchParamsObject} obj
14
+ * @param {string} prefix
15
+ * @returns
16
+ */
17
+ declare function buildSearchParams(obj: SearchParamsObject, prefix?: string): import("node:url").URLSearchParams;
18
+ declare function safeParse(text?: string): {
19
+ body: JsonValue;
20
+ isJson: boolean;
21
+ } | {
22
+ isJson?: undefined;
23
+ body: string;
24
+ };
25
+ /**
26
+ *
27
+ * @param {RequestOptions} requestOptions
28
+ */
29
+ declare function resolveUrl(requestOptions: RequestOptions): string;
30
+ /**
31
+ *
32
+ * @param {RequestOptions} requestOptions
33
+ * @param {IncomingMessage} res
34
+ * @param {RequestResponse['body']} [body]
35
+ * @param {RequestResponse['isJson']} [isJson]
36
+ * @param {RequestResponse['verbose']} [verbose]
37
+ * @returns
38
+ */
39
+ declare function buildResponse(requestOptions: RequestOptions, res: IncomingMessage, success?: boolean, body?: RequestResponse['body'], isJson?: RequestResponse['isJson'], verbose?: RequestResponse['verbose']): {
40
+ success: boolean;
41
+ status: number;
42
+ headers: import("node:http").IncomingHttpHeaders;
43
+ body: JsonValue | undefined;
44
+ isJson: boolean | undefined;
45
+ requestOptions: RequestOptions;
46
+ verbose: string[] | undefined;
47
+ };
@@ -0,0 +1,10 @@
1
+ declare const _exports: {
2
+ request: typeof request;
3
+ requestHeaders: typeof requestHeaders;
4
+ requestFile: typeof requestFile;
5
+ buildSearchParams: (obj: import("./types").SearchParamsObject, prefix?: string) => import("node:url").URLSearchParams;
6
+ };
7
+ export = _exports;
8
+ import request = require("./methods/request");
9
+ import requestFile = require("./methods/request-file");
10
+ import requestHeaders = require("./methods/request-headers");
@@ -0,0 +1,13 @@
1
+ /** @import { RequestOptions, RequestResponse } from '../types' */
2
+ export = requestFile;
3
+ import type { RequestOptions, RequestResponse } from '../types';
4
+ import { WriteStream } from "node:fs";
5
+ /**
6
+ *
7
+ * @param {RequestOptions} requestOptions
8
+ * @param {WriteStream} stream
9
+ *
10
+ * @returns {Promise<RequestResponse>}
11
+ *
12
+ */
13
+ declare function requestFile(requestOptions: RequestOptions, stream: WriteStream): Promise<RequestResponse>;
@@ -0,0 +1,11 @@
1
+ /** @import { RequestOptions, RequestResponse } from '../types' */
2
+ export = requestHeaders;
3
+ import type { RequestOptions, RequestResponse } from '../types';
4
+ /**
5
+ *
6
+ * @param {RequestOptions} requestOptions
7
+ *
8
+ * @returns {Promise<RequestResponse>}
9
+ *
10
+ */
11
+ declare function requestHeaders(requestOptions: RequestOptions): Promise<RequestResponse>;
@@ -0,0 +1,10 @@
1
+ /** @import { RequestOptions, RequestResponse } from '../types' */
2
+ export = request;
3
+ import type { RequestOptions, RequestResponse } from '../types';
4
+ /**
5
+ *
6
+ * @param {RequestOptions} requestOptions
7
+ *
8
+ * @returns {Promise<RequestResponse>}
9
+ */
10
+ declare function request(requestOptions: RequestOptions): Promise<RequestResponse>;
@@ -0,0 +1,29 @@
1
+ import { IncomingHttpHeaders, IncomingMessage } from "node:http";
2
+ type SearchParamsValue = string | number | boolean | null | undefined | SearchParamsObject | unknown[];
3
+ export interface SearchParamsObject {
4
+ [key: string]: SearchParamsValue;
5
+ }
6
+ export interface RequestOptions {
7
+ headers?: Record<string, string>;
8
+ search?: Record<string, string>;
9
+ json?: unknown;
10
+ method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD";
11
+ path: string;
12
+ baseUrl?: string;
13
+ timeoutMs?: number;
14
+ maxRedirects?: number;
15
+ }
16
+ export type RequestHandler = (res: IncomingMessage, resolve: (value: RequestResponse) => void, reject: (err: Error) => void) => void;
17
+ export type JsonValue = string | number | boolean | null | JsonValue[] | {
18
+ [key: string]: JsonValue;
19
+ };
20
+ export interface RequestResponse {
21
+ success?: boolean;
22
+ status: number;
23
+ headers: IncomingHttpHeaders;
24
+ body?: JsonValue;
25
+ isJson?: boolean;
26
+ requestOptions: RequestOptions;
27
+ verbose?: string[];
28
+ }
29
+ export {};