Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript rewrite #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions .babelrc

This file was deleted.

38 changes: 0 additions & 38 deletions .jshintrc

This file was deleted.

29 changes: 12 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
"bin": "bin/essence",
"scripts": {
"start": "npm run test-watch",
"build": "babel src --out-dir lib",
"test": "mocha --recursive --compilers js:babel-register",
"test-watch": "mocha --watch --recursive --compilers js:babel-register",
"try": "node ./bin/essence",
"build": "tsc",
"test": "cross-env TS_NODE_COMPILER_OPTIONS={\\\"module\\\":\\\"commonjs\\\"} mocha --recursive --compilers ts:ts-node/register",
"test-watch": "npm run test -- --watch",
"prepublish": "npm run test && npm run build"
},
"dependencies": {
"@types/chai-as-promised": "^7.1.0",
"axios": "^0.9.1",
"cheerio": "^0.20.0",
"commander": "^2.9.0",
Expand All @@ -25,22 +27,15 @@
"xml2js": "^0.4.16"
},
"devDependencies": {
"babel": "^6.5.2",
"babel-cli": "^6.7.5",
"babel-core": "^6.7.6",
"babel-eslint": "^6.0.2",
"babel-plugin-transform-runtime": "^6.7.5",
"babel-preset-es2015": "^6.6.0",
"babel-preset-stage-2": "^6.5.0",
"babel-register": "^6.7.2",
"babel-runtime": "^6.6.1",
"@types/chai": "^4.0.4",
"@types/mocha": "^2.2.43",
"chai": "^3.5.0",
"chai-as-promised": "^5.3.0",
"eslint": "^2.8.0",
"eslint-config-vtech": "^0.1.1",
"eslint-loader": "^1.3.0",
"eslint-plugin-babel": "^3.2.0",
"cross-env": "^5.0.5",
"mocha": "^2.4.5",
"sinon": "^2.2.0"
"sinon": "^2.2.0",
"ts-node": "^3.3.0",
"tslint-immutable": "^4.4.0",
"typescript": "^2.5.3"
}
}
40 changes: 40 additions & 0 deletions src/Container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {Map} from 'immutable';
import {memoize} from 'lodash';



interface Factory {
(get): any;
}

export default class Container {
private factories: Map<string, Factory>;

constructor(factories = Map<string, Factory>()) {
this.factories = factories;
this.get = this.get.bind(this);
}

// Executes the factory registered for the given key and
// returns its result.
get<T>(key): T {
const factory = this.factories.get(key);

if (!factory) {
throw new Error(`No factory found for key '${key}'`);
}

return factory(this.get);
}

// Returns a new Container with the given factory.
with(key, factory) {
return new Container(this.factories.set(key, factory));
}

// Returns a new Container with the given factory.
// The factory will be memoized.
withUnique(key, factory) {
return this.with(key, memoize(factory));
}
}
36 changes: 36 additions & 0 deletions src/Payload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Request from './Request';
import Response from './Response';



interface UpdateRequest{
(r: Request): Request;
}

interface UpdateResponse{
(r: Response): Response;
}

export default class Payload {

constructor(readonly req: Request, readonly res: Response) {}

static from(url: string) {
return new Payload(
new Request(url),
new Response()
);
}

withRequest(req: Request | UpdateRequest) {
return (req instanceof Request)
? new Payload(req, this.res)
: new Payload(req(this.req), this.res);
}

withResponse(res: Response | UpdateResponse) {
return (res instanceof Response)
? new Payload(this.req, res)
: new Payload(this.req, res(this.res));
}
}
10 changes: 10 additions & 0 deletions src/Request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@



export default class Request {
constructor(readonly url: string) {}

withUrl(url) {
return new Request(url);
}
}
93 changes: 93 additions & 0 deletions src/Response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import {Map} from 'immutable';
import {set, reduce} from 'lodash';



export default class Response {
private props: Map<string, ReadonlyArray<string>>;

constructor(props = Map<string, ReadonlyArray<string>>()) {
this.props = props;
}

isEmpty() {
return !this.props.size;
}

has(key) {
return this.props.has(key);
}

get(key, missing?) {
return this.first(key, missing);
}

// Returns the first data available for the given key.
first(key, missing?) {
const all = this.all(key);

return all.length
? all[0]
: missing;
}

// Returns all data available for the given key.
all(key) {
return this.props.get(key, []);
}

// Returns all keys that have any associated data.
keys() {
return this.props.keySeq().toArray();
}

// Returns number of data associated to the given key.
count(key) {
return this.all(key).length;
}

groups(keys) {
const groups = [];

keys.forEach((key) => {
this.all(key).forEach((value, i) => {
set(groups, [i, key], value);
});
});

return groups;
}

allGroups() {
return this.groups(this.keys());
}

// Returns a new response with the given prop.
withProp(key, value) {
const all = this.all(key);
const values = all.concat(value);

return new Response(
this.props.set(key, values)
);
}

// Returns a new response with the given props.
withProps(newProps) {
return reduce(
newProps,
(response, value, key) =>
response.withProp(key, value),
new Response(this.props)
);
}

// Returns a JSON representation of the response.
toJson(spaces) {
return JSON.stringify(
this.allGroups(),
null,
spaces
);
}
}
22 changes: 0 additions & 22 deletions src/condition.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/condition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Payload from './payload';
import Middleware from './middleware';



// Creates a condition that will execute the given middleware
// if the filter returns true.
export default function condition<T>(
filter: (T) => boolean,
middleware: Middleware<T>
): Middleware<T> {
return async (payload) =>
filter(payload)
? await middleware(payload)
: payload;
};
9 changes: 0 additions & 9 deletions src/conditions/isResponseEmpty.js

This file was deleted.

7 changes: 7 additions & 0 deletions src/conditions/isResponseEmpty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Payload from '../Payload';



export default function isResponseEmpty(payload: Payload): boolean {
return payload.res.isEmpty();
}
10 changes: 0 additions & 10 deletions src/conditions/requestUrlMatchesRegex.js

This file was deleted.

10 changes: 10 additions & 0 deletions src/conditions/requestUrlMatchesRegex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Payload from '../Payload';



export default function requestUrlMatchesRegex(
regex: RegExp,
payload: Payload
): boolean {
return regex.test(payload.req.url);
}
Loading