Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Add typings #226

Merged
merged 4 commits into from Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions 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
```
28 changes: 28 additions & 0 deletions 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('<div>Fragment 1</div>');
})
.listen(8081, function() {
console.log('Fragment Server listening on port 8081');
});
3 changes: 3 additions & 0 deletions examples/basic-typescript/templates/index.html
@@ -0,0 +1,3 @@
<!-- Tailor needs an index.html -->
<h1>Basic</h1>
<fragment src="http://localhost:8081"></fragment>
23 changes: 23 additions & 0 deletions 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"
]
}
57 changes: 57 additions & 0 deletions index.d.ts
@@ -0,0 +1,57 @@
/// <reference types="node" />

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<object>
, fetchTemplate?: (templatesPath: string, baseTemplateFn: (path: string) => string) => Promise<any>
, 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<ServerResponse>
, 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
}