From a12404d5fcbba52f93064461a1c309d67a193af4 Mon Sep 17 00:00:00 2001 From: arhont375 Date: Mon, 23 Apr 2018 19:21:30 +0900 Subject: [PATCH] Add typings (#226) * Add types definition file * Add example in typescript * Add readme to typescript example * Fix typos. Define type for attributes --- examples/basic-typescript/README.md | 26 +++++++++ examples/basic-typescript/index.ts | 28 +++++++++ .../basic-typescript/templates/index.html | 3 + examples/basic-typescript/tsconfig.json | 23 ++++++++ index.d.ts | 57 +++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 examples/basic-typescript/README.md 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 create mode 100644 index.d.ts diff --git a/examples/basic-typescript/README.md b/examples/basic-typescript/README.md new file mode 100644 index 0000000..5417ebf --- /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 +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/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 diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..737b688 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,57 @@ +/// + +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: 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: Attributes) => object + , pipeInstanceName?: string + , 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 +} + +