wooks 0.0.1-beta.4 → 0.0.1-beta.6

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/README.md CHANGED
@@ -306,6 +306,16 @@ app.get('test', async () => {
306
306
  })
307
307
  ```
308
308
 
309
+ Another hook for set header
310
+
311
+ ```js
312
+ import { useRespHeader } from 'wooks'
313
+ app.get('test', async () => {
314
+ const server = useRespHeader('server')
315
+ server.value = 'myServer v1.0'
316
+ })
317
+ ```
318
+
309
319
  ### Response Cookies (Set-Cookie)
310
320
 
311
321
  A function `useSetCookies` provides variety of set-cookie helpers:
@@ -332,6 +342,25 @@ app.get('test', async () => {
332
342
  })
333
343
  ```
334
344
 
345
+ Another hook for set-cookie
346
+
347
+ ```js
348
+ import { useRespCookie } from 'wooks'
349
+ app.get('test', async () => {
350
+ const session = useRespCookie('session')
351
+ session.value = 'value'
352
+ session.attrs = {
353
+ expires: '2029-01-01', // Date | string | number;
354
+ maxAge: '1h', // number | TProstoTimeMultiString;
355
+ domain: 'my-domain', // string;
356
+ path: '/home', // string;
357
+ secure: true, // boolean;
358
+ httpOnly: false, // boolean;
359
+ sameSite: true, // boolean | 'Lax' | 'None' | 'Strict';
360
+ }
361
+ })
362
+ ```
363
+
335
364
  ### Response Status
336
365
 
337
366
  It's possible to control the response status via `status` function that is available in `useResponse()`
@@ -346,6 +375,16 @@ app.get('test', async () => {
346
375
  })
347
376
  ```
348
377
 
378
+ Another status hook:
379
+ ```js
380
+ import { useStatus } from 'wooks'
381
+ app.get('test', async () => {
382
+ const status = useStatus()
383
+ status.value = 201
384
+ return 'response with status 201'
385
+ })
386
+ ```
387
+
349
388
  ### Cache-Control
350
389
 
351
390
  `useSetCacheControl` function provides helpers for headers responsible for cache control
@@ -376,6 +415,50 @@ app.get('static/*', () => {
376
415
  })
377
416
  ```
378
417
 
418
+ ### Proxy Requests
419
+
420
+ Wooks provides support for proxy request/response
421
+
422
+ ```js
423
+ import { useProxy, useRequest } from 'wooks'
424
+ //...
425
+ app.get('*', async () => {
426
+ const proxy = useProxy()
427
+ const { url } = useRequest()
428
+ const fetchResponse = await proxy('https://www.google.com' + url, {
429
+ // optional method, be default is set with
430
+ // the original request method
431
+ method: 'GET',
432
+
433
+ // the next four options help to filter out
434
+ // request/response headers/cookies
435
+ // each of the option accepts an object with:
436
+ // - allow: '*' | (string | RegExp)[] - a list to allow (default '*')
437
+ // - block: '*' | (string | RegExp)[] - a list to block
438
+ // - overwrite: Record<string| strgin> | ((data: object) -> object) - object or fn to overwrite data
439
+ reqHeaders: { block: ['referer'] },
440
+ reqCookies: { allow: ['cookie-to-pass-downstream'] },
441
+ resHeaders: { overwrite: { 'x-proxied-by': 'wooks-proxy' } },
442
+ resCookies: { allow: ['cookie-to-pass-upstream'] },
443
+
444
+ // debug: true - will print proxy paths and headers/cookies
445
+ debug: true,
446
+ })
447
+ return fetchResponse // fetch response is supported, the body will be upstreamed
448
+
449
+ // > you can also return fully buffered body as Uint8Array
450
+ // return new Uint8Array(await fetchResponse.arrayBuffer())
451
+
452
+ // > or as string
453
+ // return fetchResponse.text()
454
+
455
+ // > or change response before return
456
+ // const data = await fetchResponse.text() + '<new data>'
457
+ // return data
458
+ })
459
+ //...
460
+ ```
461
+
379
462
  ### Serve File (Serve-Static)
380
463
 
381
464
  Function `serveFile` returns a readable stream and prepares all the neccessary response headers (like content-length, content-type etc).
@@ -5,7 +5,7 @@ export declare function useSetHeaders(): {
5
5
  setHeader: (name: string, value: string | number) => void;
6
6
  removeHeader: (name: string) => void;
7
7
  setContentType: (value: string) => void;
8
- headers: Record<string, string>;
8
+ headers: Record<string, string | string[]>;
9
9
  enableCors: (origin?: string) => void;
10
10
  };
11
11
  //# sourceMappingURL=headers.d.ts.map
@@ -9,4 +9,5 @@ export * from './body';
9
9
  export * from './core-test';
10
10
  export { useCurrentWooksContext, useCacheObject, clearCacheObject, } from './core';
11
11
  export * from './hooks';
12
+ export * from './proxy';
12
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/composables/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,wBAAwB,CAAA;AACtC,cAAc,4BAA4B,CAAA;AAC1C,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,QAAQ,CAAA;AACtB,cAAc,aAAa,CAAA;AAC3B,OAAO,EACH,sBAAsB,EACtB,cAAc,EACd,gBAAgB,GACnB,MAAM,QAAQ,CAAA;AACf,cAAc,SAAS,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/composables/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,wBAAwB,CAAA;AACtC,cAAc,4BAA4B,CAAA;AAC1C,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,QAAQ,CAAA;AACtB,cAAc,aAAa,CAAA;AAC3B,OAAO,EACH,sBAAsB,EACtB,cAAc,EACd,gBAAgB,GACnB,MAAM,QAAQ,CAAA;AACf,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
@@ -0,0 +1,25 @@
1
+ /// <reference types="node" />
2
+ import { IncomingHttpHeaders } from 'http';
3
+ declare class IterableRecords {
4
+ [Symbol.iterator](): this;
5
+ protected index: number;
6
+ next(): IteratorResult<[string, string]>;
7
+ }
8
+ export declare class CookiesIterable extends IterableRecords {
9
+ private cookies;
10
+ constructor(cookiesString: string);
11
+ next(): IteratorResult<[string, string]>;
12
+ }
13
+ export declare class HeadersIterable extends IterableRecords {
14
+ private entries;
15
+ constructor(headers: Record<string, string> | IncomingHttpHeaders);
16
+ next(): IteratorResult<[string, string]>;
17
+ }
18
+ export interface TWooksProxyControls {
19
+ overwrite?: Record<string, string> | ((data: Record<string, string>) => Record<string, string>);
20
+ allow?: (string | RegExp)[] | '*';
21
+ block?: (string | RegExp)[] | '*';
22
+ }
23
+ export declare function applyProxyControls(records: IterableIterator<[string, string]>, controls: TWooksProxyControls, additionalBlockers?: string[]): Record<string, string>;
24
+ export {};
25
+ //# sourceMappingURL=proxy-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy-utils.d.ts","sourceRoot":"","sources":["../../../src/composables/proxy-utils.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,MAAM,CAAA;AAE1C,cAAM,eAAe;IACjB,CAAC,MAAM,CAAC,QAAQ,CAAC;IAIjB,SAAS,CAAC,KAAK,SAAI;IAEnB,IAAI,IAAI,cAAc,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAG3C;AAED,qBAAa,eAAgB,SAAQ,eAAe;IAChD,OAAO,CAAC,OAAO,CAAU;gBAEb,aAAa,EAAE,MAAM;IAKjC,IAAI,IAAI,cAAc,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAO3C;AAED,qBAAa,eAAgB,SAAQ,eAAe;IAChD,OAAO,CAAC,OAAO,CAAoB;gBAEvB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,mBAAmB;IAKjE,IAAI,IAAI,cAAc,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAK3C;AAED,MAAM,WAAW,mBAAmB;IAChC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;IAC/F,KAAK,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,GAAG,GAAG,CAAA;IACjC,KAAK,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,GAAG,GAAG,CAAA;CACpC;AAED,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,QAAQ,EAAE,mBAAmB,EAAE,kBAAkB,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CA0BpK"}
@@ -0,0 +1,12 @@
1
+ import { TWooksProxyControls } from './proxy-utils';
2
+ export { TWooksProxyControls } from './proxy-utils';
3
+ export declare function useProxy(): (target: string, opts?: TWooksProxyOptions) => Promise<Response>;
4
+ export interface TWooksProxyOptions {
5
+ method?: string;
6
+ reqHeaders?: TWooksProxyControls;
7
+ reqCookies?: TWooksProxyControls;
8
+ resHeaders?: TWooksProxyControls;
9
+ resCookies?: TWooksProxyControls;
10
+ debug?: boolean;
11
+ }
12
+ //# sourceMappingURL=proxy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../../../src/composables/proxy.ts"],"names":[],"mappings":"AAIA,OAAO,EAAwD,mBAAmB,EAAE,MAAM,eAAe,CAAA;AACzG,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AAgBnD,wBAAgB,QAAQ,aAKgB,MAAM,SAAS,kBAAkB,KAAG,QAAQ,QAAQ,CAAC,CAkE5F;AAED,MAAM,WAAW,kBAAkB;IAC/B,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,UAAU,CAAC,EAAE,mBAAmB,CAAA;IAChC,UAAU,CAAC,EAAE,mBAAmB,CAAA;IAChC,UAAU,CAAC,EAAE,mBAAmB,CAAA;IAChC,UAAU,CAAC,EAAE,mBAAmB,CAAA;IAChC,KAAK,CAAC,EAAE,OAAO,CAAA;CAClB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=proxy.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.spec.d.ts","sourceRoot":"","sources":["../../../../src/composables/tests/proxy.spec.ts"],"names":[],"mappings":""}
@@ -24,7 +24,7 @@ export declare class BaseWooksResponse<BodyType = unknown> {
24
24
  setHeader(name: string, value: string): this;
25
25
  getHeader(name: string): string | string[];
26
26
  protected mergeHeaders(): this;
27
- protected mergeStatus(renderedBody: string): this;
27
+ protected mergeStatus(renderedBody: string | Uint8Array): this;
28
28
  respond(): void;
29
29
  }
30
30
  //# sourceMappingURL=core.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../../../src/response/core.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAA;AAEtD,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,EAAgB,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAErE,OAAO,EAAsB,aAAa,EAAE,MAAM,wBAAwB,CAAA;AAY1E,qBAAa,iBAAiB,CAAC,QAAQ,GAAG,OAAO;IACjC,SAAS,CAAC,QAAQ,EAAE,yBAAyB;gBAAnC,QAAQ,GAAE,yBAAwC;IAExE,SAAS,CAAC,OAAO,EAAE,eAAe,CAAI;IAEtC,SAAS,CAAC,KAAK,CAAC,EAAE,QAAQ,CAAA;IAE1B,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,CAAK;IAE1D,IAAI,MAAM,IAIQ,eAAe,CAFhC;IAED,IAAI,MAAM,CAAC,KAAK,EAAE,eAAe,EAEhC;IAED,IAAI,IAAI,IAIQ,QAAQ,GAAG,SAAS,CAFnC;IAED,IAAI,IAAI,CAAC,KAAK,EAAE,QAAQ,GAAG,SAAS,EAEnC;IAED,SAAS,CAAC,KAAK,EAAE,eAAe;IAKhC,OAAO,CAAC,KAAK,EAAE,QAAQ;IAKvB,cAAc;IAId,cAAc,CAAC,KAAK,E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AC,KAAK,EAAE,QAAQ,GAAG,SAAS,EAEnC;IAED,SAAS,CAAC,KAAK,EAAE,eAAe;IAKhC,OAAO,CAAC,KAAK,EAAE,QAAQ;IAKvB,cAAc;IAId,cAAc,CAAC,KAAK,EAAE,MAAM;IAK5B,UAAU,CAAC,MAAM,GAAE,MAAY;IAK/B,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO,CAAC,iBAAiB,CAAC;IAMzE,eAAe,CAAC,IAAI,EAAE,aAAa;IAInC,YAAY,CAAC,QAAQ,EAAE,MAAM;IAM7B,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAKlC,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAIrC,SAAS,CAAC,IAAI,EAAE,MAAM;IAItB,SAAS,CAAC,YAAY;IAoBtB,SAAS,CAAC,WAAW,CAAC,YAAY,EAAE,MAAM,GAAG,UAAU;IASvD,OAAO;CAgDV"}
@@ -1,8 +1,8 @@
1
1
  import { BaseWooksResponse } from './core';
2
2
  export declare class BaseWooksResponseRenderer<T = unknown> implements TWooksResponseRenderer<T> {
3
- render(response: BaseWooksResponse<T>): string;
3
+ render(response: BaseWooksResponse<T>): string | Uint8Array;
4
4
  }
5
5
  export interface TWooksResponseRenderer<T = unknown> {
6
- render: (response: BaseWooksResponse<T>) => string;
6
+ render: (response: BaseWooksResponse<T>) => string | Uint8Array;
7
7
  }
8
8
  //# sourceMappingURL=renderer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"renderer.d.ts","sourceRoot":"","sources":["../../../src/response/renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAA;AAI1C,qBAAa,yBAAyB,CAAC,CAAC,GAAG,OAAO,CAAE,YAAW,sBAAsB,CAAC,CAAC,CAAC;IACpF,MAAM,CAAC,QAAQ,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,MAAM;CAcjD;AAED,MAAM,WAAW,sBAAsB,CAAC,CAAC,GAAG,OAAO;IAC/C,MAAM,EAAE,CAAC,QAAQ,EAAE,iBAAiB,CAAC,CAAC,CAAC,KAAK,MAAM,CAAA;CACrD"}
1
+ {"version":3,"file":"renderer.d.ts","sourceRoot":"","sources":["../../../src/response/renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAA;AAI1C,qBAAa,yBAAyB,CAAC,CAAC,GAAG,OAAO,CAAE,YAAW,sBAAsB,CAAC,CAAC,CAAC;IACpF,MAAM,CAAC,QAAQ,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,MAAM,GAAG,UAAU;CAiB9D;AAED,MAAM,WAAW,sBAAsB,CAAC,CAAC,GAAG,OAAO;IAC/C,MAAM,EAAE,CAAC,QAAQ,EAAE,iBAAiB,CAAC,CAAC,CAAC,KAAK,MAAM,GAAG,UAAU,CAAA;CAClE"}
@@ -0,0 +1,5 @@
1
+ export declare function log(text: string): void;
2
+ export declare function logBright(text: string): void;
3
+ export declare function warn(text: string): void;
4
+ export declare function logError(error: string): void;
5
+ //# sourceMappingURL=log.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"log.d.ts","sourceRoot":"","sources":["../../../src/utils/log.ts"],"names":[],"mappings":"AAGA,wBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,QAE/B;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,QAErC;AAED,wBAAgB,IAAI,CAAC,IAAI,EAAE,MAAM,QAEhC;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,QAErC"}