From 7e1fb0fe23ee79ce55bf8b7889a3dd7505b1192f Mon Sep 17 00:00:00 2001 From: arhont375 Date: Mon, 16 Apr 2018 22:59:22 +0900 Subject: [PATCH 1/4] Add types definition file --- index.d.ts | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..1314bb8 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,47 @@ +/// + +import { EventEmitter } from "events"; +import { Stream } from "stream"; +import { Url } from "url"; +import { IncomingMessage, ServerResponse } from "http"; + +export = Tailor; + +declare class Tailor extends EventEmitter { + /** + * Creates new instance of Tailor + * @param options Parameters to pass to Tailor + * @param options.amdLoaderUrl URL to AMD loader. Default is RequireJS from cdnjs. + * @param options.fetchContext Function that should fetch the template, call parseTemplate and return a promise of the result. Useful to implement your own way to retrieve and cache the templates. Default implementation: serve templates from local path + * @param options.fetchTemplate Function that should fetch the template, call parseTemplate and return a promise of the result + * @param options.filterRequestHeaders Function that filters the request headers that are passed to fragment request + * @param options.filterResponseHeaders Function that maps the given response headers from the primary fragment request to the final response + * @param options.fragmentTag Name of fragment tag + * @param options.handleTags Array of custom tags. + * @param options.handleTag Receives a tag or closing tag and serializes it to a string or returns as stream + * @param options.maxAssetLinks Number of allowed link headers of CSS and JS for per fragment + * @param options.pipeAttributes Function that returns the minimal set of fragment attributes available on the frontend + * @param options.pipeInstanceName Name of pipe instance that available in the browser window object to consume frontend hooks + * @param options.requestFragment Function that returns a promise of request to a fragment server + * @param options.templatesPath Path to local templates + */ + constructor(options?: { + amdLoaderUrl?: string + , fetchContext?: (req: IncomingMessage) => Promise + , fetchTemplate?: (templatesPath: string, baseTemplateFn: (path: string) => string) => Promise + , filterRequestHeaders?: (attributes: object, req: IncomingMessage) => object + , filterResponseHeaders?: (attributes: object, req: ServerResponse) => object + , fragmentTag?: string + , handledTags?: string[] + , handleTag?: (request: IncomingMessage, tag: object, options: object, context: object) => Stream | string + , maxAssetLinks?: number + , pipeAttributes?: (attributes: object) => object + , pipeInstanceName?: string + , requestFragment?: (filterHeaders: (attributes: object, req: IncomingMessage) => object, url: Url, attributes: object, req: IncomingMessage) => Promise + , templatesPath?: string + }) + + requestHandler(request: IncomingMessage, response: ServerResponse): void; +} + + From 40606405622d023f4f43de416add5837d824cb83 Mon Sep 17 00:00:00 2001 From: arhont375 Date: Mon, 16 Apr 2018 22:59:53 +0900 Subject: [PATCH 2/4] Add example in typescript --- examples/basic-typescript/index.ts | 28 +++++++++++++++++++ .../basic-typescript/templates/index.html | 3 ++ examples/basic-typescript/tsconfig.json | 23 +++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 examples/basic-typescript/index.ts create mode 100644 examples/basic-typescript/templates/index.html create mode 100644 examples/basic-typescript/tsconfig.json diff --git a/examples/basic-typescript/index.ts b/examples/basic-typescript/index.ts new file mode 100644 index 0000000..f412221 --- /dev/null +++ b/examples/basic-typescript/index.ts @@ -0,0 +1,28 @@ +import * as http from 'http'; + +import Tailor = require('../../index'); + +const tailor = new Tailor({ + templatesPath: __dirname + '/templates' +}) + +// Root Server +http + .createServer((req, res) => { + tailor.requestHandler(req, res) + }) + .listen(8080, function() { + console.log('Tailor server listening on port 8080'); + }); + +// Fragment server - Any http server that can serve fragments +http + .createServer((req, res) => { + res.writeHead(200, { + 'Content-Type': 'text/html' + }); + res.end('
Fragment 1
'); + }) + .listen(8081, function() { + console.log('Fragment Server listening on port 8081'); + }); diff --git a/examples/basic-typescript/templates/index.html b/examples/basic-typescript/templates/index.html new file mode 100644 index 0000000..0c8586f --- /dev/null +++ b/examples/basic-typescript/templates/index.html @@ -0,0 +1,3 @@ + +

Basic

+ diff --git a/examples/basic-typescript/tsconfig.json b/examples/basic-typescript/tsconfig.json new file mode 100644 index 0000000..c1fae14 --- /dev/null +++ b/examples/basic-typescript/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "./index.ts", + "./../../index.d.ts" + ] +} \ No newline at end of file From a8d87c6f3a9e8f6b9a114d1f9e522dbc57a2d351 Mon Sep 17 00:00:00 2001 From: arhont375 Date: Tue, 17 Apr 2018 20:16:21 +0900 Subject: [PATCH 3/4] Add readme to typescript example --- examples/basic-typescript/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 examples/basic-typescript/README.md diff --git a/examples/basic-typescript/README.md b/examples/basic-typescript/README.md new file mode 100644 index 0000000..d6658b2 --- /dev/null +++ b/examples/basic-typescript/README.md @@ -0,0 +1,26 @@ +# Basis-typescript + +This example shows how to use Tailor with Typescript + +## Run + +Basically there 2 ways to use node with typescript. +In both cases types will be checked and from external point of view they should behave identically. + +### With ts-node + +Install ts-node and just run this file: + +```bash +ts-node ./index.ts +``` + +### Transpile to javascript + +Install TypeScript compiler and execute following; + +```bash +tsk ./index.ts +# Now we have index.js and can run it with node.js +node ./index.js +``` \ No newline at end of file From d0e0ca62baaec9c5289e536daea390d729481494 Mon Sep 17 00:00:00 2001 From: arhont375 Date: Thu, 19 Apr 2018 22:12:56 +0900 Subject: [PATCH 4/4] Fix typos. Define type for attributes --- examples/basic-typescript/README.md | 2 +- index.d.ts | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/examples/basic-typescript/README.md b/examples/basic-typescript/README.md index d6658b2..5417ebf 100644 --- a/examples/basic-typescript/README.md +++ b/examples/basic-typescript/README.md @@ -20,7 +20,7 @@ ts-node ./index.ts Install TypeScript compiler and execute following; ```bash -tsk ./index.ts +tsc ./index.ts # Now we have index.js and can run it with node.js node ./index.js ``` \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 1314bb8..737b688 100644 --- a/index.d.ts +++ b/index.d.ts @@ -29,19 +29,29 @@ declare class Tailor extends EventEmitter { amdLoaderUrl?: string , fetchContext?: (req: IncomingMessage) => Promise , fetchTemplate?: (templatesPath: string, baseTemplateFn: (path: string) => string) => Promise - , filterRequestHeaders?: (attributes: object, req: IncomingMessage) => object - , filterResponseHeaders?: (attributes: object, req: ServerResponse) => object + , filterRequestHeaders?: (attributes: Attributes, req: IncomingMessage) => object + , filterResponseHeaders?: (attributes: Attributes, res: ServerResponse) => object , fragmentTag?: string , handledTags?: string[] , handleTag?: (request: IncomingMessage, tag: object, options: object, context: object) => Stream | string , maxAssetLinks?: number - , pipeAttributes?: (attributes: object) => object + , pipeAttributes?: (attributes: Attributes) => object , pipeInstanceName?: string - , requestFragment?: (filterHeaders: (attributes: object, req: IncomingMessage) => object, url: Url, attributes: object, req: IncomingMessage) => Promise + , requestFragment?: (filterHeaders: (attributes: Attributes, req: IncomingMessage) => object, url: Url, attributes: Attributes, req: IncomingMessage) => Promise , templatesPath?: string }) requestHandler(request: IncomingMessage, response: ServerResponse): void; } +interface Attributes { + id: string, + src: string + async?: boolean + fallbackUrl?: string, + primary?: boolean + public?: boolean + [key: string]: any +} +