@fastly/expressly 1.0.0-alpha.1 → 1.0.0-alpha.4

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
@@ -17,11 +17,11 @@ First, head over to [developer.fastly.com](https://developer.fastly.com) to get
17
17
  Install expressly from the [npm registry](https://www.npmjs.com/package/@fastly/expressly):
18
18
 
19
19
  ```shell
20
- npm i @fastly/expressly
20
+ npm i @fastly/expressly@1.0.0-alpha.4
21
21
  ```
22
22
 
23
23
  ```shell
24
- yarn add @fastly/expressly
24
+ yarn add @fastly/expressly@1.0.0-alpha.4
25
25
  ```
26
26
 
27
27
  ### Your first expressly app
@@ -1,14 +1,13 @@
1
1
  /// <reference types="@fastly/js-compute" />
2
- import { ECommonObject } from "../common";
3
2
  import { CookieMap } from "./cookie-map";
4
3
  import { EConfig } from "..";
5
- export declare class ERequest extends ECommonObject {
4
+ export declare class ERequest extends Request {
6
5
  private config;
7
6
  private event;
8
7
  readonly clientInfo: ClientInfo;
9
8
  readonly method: string;
10
9
  headers: Headers;
11
- url: URL;
10
+ urlObj: URL;
12
11
  query: URLSearchParams;
13
12
  params: {
14
13
  [key: string]: string;
@@ -21,7 +20,11 @@ export declare class ERequest extends ECommonObject {
21
20
  get secure(): boolean;
22
21
  get subdomains(): Array<string>;
23
22
  get hostname(): string;
24
- json(): Promise<any>;
25
- text(): Promise<string>;
26
- arrayBuffer(): Promise<ArrayBuffer>;
23
+ set(headerNameOrObject: string | {
24
+ [key: string]: string;
25
+ }, value?: string): void;
26
+ private appendHeader;
27
+ append(headerNameOrObject: string | {
28
+ [key: string]: string | string[];
29
+ }, value?: string | string[]): void;
27
30
  }
@@ -1,47 +1,70 @@
1
- import { ECommonObject } from "../common";
2
1
  import { CookieMap } from "./cookie-map";
3
- export class ERequest extends ECommonObject {
2
+ export class ERequest extends Request {
4
3
  constructor(config, event) {
5
- super();
4
+ super(event.request);
6
5
  this.config = config;
7
6
  this.event = event;
8
7
  this.params = {};
9
8
  this.clientInfo = event.client;
10
- this.method = event.request.method;
11
- this.url = new URL(event.request.url);
12
- this.query = this.url.searchParams;
13
- this.headers = event.request.headers;
9
+ this.urlObj = new URL(event.request.url);
10
+ this.query = this.urlObj.searchParams;
14
11
  // Parse cookies.
15
12
  if (this.config.parseCookie) {
16
13
  this.cookies = new CookieMap(this.headers);
17
14
  }
18
15
  }
19
16
  // Express-like URL helpers.
17
+ // get url(): string {
18
+ // console.log("custom getter");
19
+ // return this.urlObj.toString();
20
+ // }
20
21
  get path() {
21
- return this.url.pathname;
22
+ return this.urlObj.pathname;
22
23
  }
23
24
  get ip() {
24
25
  return this.clientInfo.address;
25
26
  }
26
27
  get protocol() {
27
- return this.url.protocol;
28
+ return this.urlObj.protocol;
28
29
  }
29
30
  get secure() {
30
- return this.url.protocol === "https";
31
+ return this.urlObj.protocol === "https";
31
32
  }
32
33
  get subdomains() {
33
- return this.url.hostname.split(".").slice(0, -2);
34
+ return this.urlObj.hostname.split(".").slice(0, -2);
34
35
  }
35
36
  get hostname() {
36
- return this.url.hostname;
37
+ return this.urlObj.hostname;
37
38
  }
38
- async json() {
39
- return await this.event.request.json();
39
+ // Header helpers.
40
+ set(headerNameOrObject, value) {
41
+ if (typeof headerNameOrObject === "string") {
42
+ this.headers.set(headerNameOrObject, value);
43
+ }
44
+ else {
45
+ Object.keys(headerNameOrObject).forEach((headerName) => {
46
+ this.headers.set(headerName, headerNameOrObject[headerName]);
47
+ });
48
+ }
40
49
  }
41
- async text() {
42
- return await this.event.request.text();
50
+ appendHeader(headerName, headerValue) {
51
+ if (typeof headerValue === "string") {
52
+ this.headers.append(headerName, headerValue);
53
+ }
54
+ else if (Array.isArray(headerValue)) {
55
+ headerValue.forEach((v) => {
56
+ this.headers.append(headerName, v);
57
+ });
58
+ }
43
59
  }
44
- async arrayBuffer() {
45
- return await this.event.request.arrayBuffer();
60
+ append(headerNameOrObject, value) {
61
+ if (typeof headerNameOrObject === "string") {
62
+ this.appendHeader(headerNameOrObject, value);
63
+ }
64
+ else {
65
+ Object.keys(headerNameOrObject).forEach((headerName) => {
66
+ this.appendHeader(headerName, headerNameOrObject[headerName]);
67
+ });
68
+ }
46
69
  }
47
70
  }
@@ -140,7 +140,7 @@ export class Router {
140
140
  pathMatcherCache.set(pattern, match(pattern, { decode: decodeURIComponent }));
141
141
  }
142
142
  // Match on pathname.
143
- let { path, params } = pathMatcherCache.get(pattern)(req.url.pathname) || {};
143
+ let { path, params } = pathMatcherCache.get(pattern)(req.urlObj.pathname) || {};
144
144
  if (path) {
145
145
  if (this.config.extractRequestParameters) {
146
146
  req.params = params;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastly/expressly",
3
- "version": "1.0.0-alpha.1",
3
+ "version": "1.0.0-alpha.4",
4
4
  "description": "Express-style router for Fastly's Compute@Edge.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,10 +0,0 @@
1
- import ERequest from "./request";
2
- import EResponse from "./response";
3
- export declare type MiddlewareCallback = (req: ERequest, res: EResponse, next?: () => void) => Promise<any>;
4
- export declare class Middleware {
5
- private matchFn;
6
- private callback;
7
- constructor(matchFn: Function, callback: MiddlewareCallback);
8
- check(event: ERequest): 0 | 404 | 405;
9
- run(req: ERequest, res: EResponse): Promise<any>;
10
- }
@@ -1,13 +0,0 @@
1
- export class Middleware {
2
- constructor(matchFn, callback) {
3
- this.matchFn = matchFn;
4
- this.callback = callback;
5
- }
6
- check(event) {
7
- return this.matchFn(event);
8
- }
9
- async run(req, res) {
10
- // Supply an empty callback as an equivalent of next() in Express.js.
11
- await this.callback(req, res, () => { });
12
- }
13
- }
@@ -1,10 +0,0 @@
1
- import ERequest from "./request";
2
- import EResponse from "./response";
3
- export declare type RequestHandlerCallback = (req: ERequest, res: EResponse) => Promise<any>;
4
- export declare class Route {
5
- private matchFn;
6
- private callback;
7
- constructor(matchFn: Function, callback: RequestHandlerCallback);
8
- check(event: ERequest): 0 | 404 | 405;
9
- run(req: ERequest, res: EResponse): Promise<any>;
10
- }
@@ -1,12 +0,0 @@
1
- export class Route {
2
- constructor(matchFn, callback) {
3
- this.matchFn = matchFn;
4
- this.callback = callback;
5
- }
6
- check(event) {
7
- return this.matchFn(event);
8
- }
9
- async run(req, res) {
10
- await this.callback(req, res);
11
- }
12
- }