Skip to content

Commit

Permalink
feat(typescript): add TypeScript annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Narazaka committed Apr 21, 2017
1 parent 06fd385 commit 8eeb561
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -3,6 +3,7 @@
"version": "2.3.1",
"description": "Pact for all things Javascript",
"main": "./src/pact.js",
"types": "./src/pact.d.ts",
"scripts": {
"clean": "rimraf docs dist coverage logs pacts jscpd.json",
"coverage": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
Expand Down
9 changes: 9 additions & 0 deletions src/common/config.d.ts
@@ -0,0 +1,9 @@
interface Config {
mockService: {
host: string;
port: number;
};
logging: boolean;
}

export = Config;
1 change: 1 addition & 0 deletions src/common/logger.d.ts
@@ -0,0 +1 @@
export function info(msg: string): void;
1 change: 1 addition & 0 deletions src/common/net.d.ts
@@ -0,0 +1 @@
export function isPortAvailable(port: number, host: string): Promise<void>;
6 changes: 6 additions & 0 deletions src/common/request.d.ts
@@ -0,0 +1,6 @@
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';

export class Request {
constructor ();
send (method: HTTPMethod, url: string, body: string): Promise<string>;
}
32 changes: 32 additions & 0 deletions src/dsl/interaction.d.ts
@@ -0,0 +1,32 @@
import {HTTPMethod} from '../common/request';
import {MatcherResult} from './matchers';

export class Interaction {
constructor();
given(providerState: string): Interaction;
uponReceiving(description: string): Interaction;
withRequest(requestOpts: RequestOptions): Interaction;
willRespondWith(responseOpts: ResponseOptions): Interaction;
json(): object;
}

export interface RequestOptions {
method: HTTPMethod;
path: string | MatcherResult;
query?: any;
headers?: {[name: string]: string | MatcherResult};
body?: any;
}

export interface ResponseOptions {
status: number | MatcherResult;
headers?: {[name: string]: string | MatcherResult};
body?: any;
}

export interface InteractionObject {
state: string;
uponReceiving: string;
withRequest: RequestOptions;
willRespondWith: ResponseOptions;
}
26 changes: 26 additions & 0 deletions src/dsl/matchers.d.ts
@@ -0,0 +1,26 @@
export interface MatcherResult {
json_class: string;
}

export function term(opts: {generate: string, matcher: string}): {
json_class: 'Pact::Term',
data: {
generate: string,
matcher: {
json_class: 'Regexp',
o: 0,
s: string,
},
},
};

export function eachLike<T>(content: T, opts?: {min: number}): {
json_class: 'Pact::ArrayLike',
contents: T,
min: number,
};

export function somethingLike<T>(value: T): {
json_class: 'Pact::SomethingLike',
contents: T,
};
4 changes: 2 additions & 2 deletions src/dsl/matchers.js
Expand Up @@ -35,7 +35,7 @@ module.exports.term = (opts) => {

/**
* The eachLike matcher
* @param {string} content
* @param {any} content
* @param {Object} opts
* @param {Number} opts.min
*/
Expand All @@ -57,7 +57,7 @@ module.exports.eachLike = (content, opts) => {

/**
* The somethingLike matcher
* @param {string} value - the value to be somethingLike
* @param {any} value - the value to be somethingLike
*/
module.exports.somethingLike = (value) => {
if (isNil(value) || isFunction(value)) {
Expand Down
9 changes: 9 additions & 0 deletions src/dsl/mockService.d.ts
@@ -0,0 +1,9 @@
import {Interaction} from './interaction';

export class MockService {
constructor (consumer: string, provider: string, port?: number, host?: string, ssl?: boolean);
addInteraction(interaction: Interaction): Promise<string>;
removeInteractions(): Promise<string>;
verify(): Promise<string>;
writePact(): Promise<string>;
}
2 changes: 2 additions & 0 deletions src/dsl/verifier.d.ts
@@ -0,0 +1,2 @@
declare function verifyProvider(opts: any): Promise<void>;
export = verifyProvider;
26 changes: 26 additions & 0 deletions src/pact-karma.d.ts
@@ -0,0 +1,26 @@
import {InteractionObject} from "./dsl/interaction";
import * as _Matchers from "./dsl/matchers";

declare function pact(opts: pact.PactKarmaOptions): pact.PactKarmaProvider;

declare namespace pact {
export interface PactKarmaOptions {
consumer: string;
provider: string;
port?: number;
host?: string;
ssl?: boolean;
}

export interface PactKarmaProvider {
addInteraction(interactionObj: InteractionObject): Promise<string>;
verify(): Promise<void>;
finalize(): Promise<void>;
writePact(): Promise<string>;
removeInteractions(): Promise<string>;
}

export const Matchers: typeof _Matchers;
}

export = pact;
36 changes: 36 additions & 0 deletions src/pact.d.ts
@@ -0,0 +1,36 @@
import {InteractionObject} from "./dsl/interaction";
import * as _Matchers from "./dsl/matchers";
import _Verifier = require("./dsl/verifier");

declare function pact(opts: pact.PactOptions): pact.PactProvider;

declare namespace pact {
export interface PactOptions {
consumer: string;
provider: string;
port?: number;
host?: string;
ssl?: boolean;
sslcert?: string;
sslkey?: string;
dir?: string;
log?: string;
logLevel?: string;
spec?: number;
cors?: boolean;
}

export interface PactProvider {
setup(): Promise<void>;
addInteraction(interactionObj: InteractionObject): Promise<string>;
verify(): Promise<void>;
finalize(): Promise<void>;
writePact(): Promise<string>;
removeInteractions(): Promise<string>;
}

export const Matchers: typeof _Matchers;
export const Verifier: typeof _Verifier;
}

export = pact;
14 changes: 14 additions & 0 deletions tsconfig.json
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"forceConsistentCasingInFileNames": true,
"noImplicitAny": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"noImplicitUseStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"strictNullChecks": true
}
}

0 comments on commit 8eeb561

Please sign in to comment.