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 type definitions #223

Merged
merged 4 commits into from
Jan 9, 2018
Merged

Conversation

mike-north
Copy link
Member

@mike-north mike-north commented Dec 16, 2017

Haven't established whether this is complete yet, but these types are working well in a few apps Pretender-heavy test suites (about 130 acceptance test modules total)

Depends on pretenderjs/FakeXMLHttpRequest#35 being merged, release cut, etc...

Early adopters, please feel free to copy/paste these type definitions into a pretender.d.ts file within your project

TODO

@mike-north
Copy link
Member Author

mike-north commented Dec 16, 2017

Usage example

import { Config, default as Server, ResponseHandler } from 'pretender';
// Requires merge/release of https://github.com/pretenderjs/FakeXMLHttpRequest/pull/35
import FakeXMLHttpRequest from "fake-xml-http-request";

/**
 * JSON object type definitions
 * >> https://github.com/mike-north/json-typescript
 */
type Arr<T> = Array<T>;
declare namespace JSON {
	type Primitive = string | number | boolean | null;
	type Value = Primitive | Object | Array;
	interface Object {
		[member: string]: Value;
	}
	interface Array extends Arr<Value> {}
}


/**
 * 
 * @param status HTTP response status code
 * @param payload 
 */
function json(status: number, payload: JSON.Value): ResponseHandler {
  return function(request) {
    return [status, { "Content-Type": "text/json" }, JSON.stringify(payload)];
  };
}

const SETUP: Config = function() {
  this.get("/api/v1/courses/:id", json(200, { data: [] }));
  this.get("/api/v1/courses/:id/stages", json(200, { data: [] }));
  this.get("/api/v1/courses", json(200, { data: [] }));
  this.get("/api/v1/events", json(200, { data: [] }));
  this.get("/api/v1/talks", json(200, { data: [] }));
  this.get("/api/v1/whoami", json(200, { data: [] }));
  this.post("/api/v1/sessions", json(200, { tok: "sdja" }));
};

const server = new Server(SETUP);

Copy link
Contributor

@stefanpenner stefanpenner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this direction!

I realize this isn't a complete typescript conversion, but I believe the two missing pieces i identified above are important for a first Pass.

Also, for future work I would be very much on-board with a complete TypeScript update of the code-base. That way types stay in-sync, and the code-base itself can benefit.

index.d.ts Outdated
export type RequestHandler = (
urlExpression: string,
response: ResponseHandler
) => void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a fourth argument here async: boolean

register: function register(verb, url, handler, async) {

index.d.ts Outdated
response: ResponseHandler
) => void;

export type ResponseData = [number, { [k: string]: string }, string];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResponseData may also be a promise of ResponseData

var result = handler.handler(request);

@stefanpenner
Copy link
Contributor

With the above two addressed :shipit:

@mike-north
Copy link
Member Author

fake-xml-http-request v2.0 has been merged (includes upstream type information). Because it drops support for end-of-life node versions, we'll need to major-version bump pretender too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants