-
Notifications
You must be signed in to change notification settings - Fork 158
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
Conversation
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); |
9e8d46b
to
4401dbe
Compare
There was a problem hiding this 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; |
There was a problem hiding this comment.
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
Line 325 in fc363b3
register: function register(verb, url, handler, async) { |
index.d.ts
Outdated
response: ResponseHandler | ||
) => void; | ||
|
||
export type ResponseData = [number, { [k: string]: string }, string]; |
There was a problem hiding this comment.
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
Line 389 in fc363b3
var result = handler.handler(request); |
With the above two addressed |
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. |
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 projectTODO