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

generate index.d.ts in build #338

Open
wants to merge 2 commits 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "3.4.7",
"main": "./dist/pretender.js",
"module": "./dist/pretender.es.js",
"types": "index.d.ts",
"types": "./dist/index.d.ts",
"description": "Pretender is a mock server library for XMLHttpRequest and Fetch, that comes with an express/sinatra style syntax for defining routes and their handlers.",
"license": "MIT",
"scripts": {
Expand Down Expand Up @@ -42,6 +42,7 @@
"release-it-lerna-changelog": "^2.3.0",
"rollup": "^2.10.2",
"rollup-plugin-typescript": "^1.0.0",
Copy link
Member

Choose a reason for hiding this comment

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

I think you need to remove this then rerun yarn.

error Your lockfile needs to be updated

"rollup-plugin-typescript2": "^0.30.0",
"sinon": "^9.0.2",
"tslib": "^1.9.3",
"typescript": "~3.1.1",
Expand Down
6 changes: 3 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import typescript from "rollup-plugin-typescript";
import typescript from "rollup-plugin-typescript2";
import { readFileSync } from "fs";
import pkg from "./package.json";

Expand Down Expand Up @@ -31,7 +31,7 @@ module.exports = [
format: "es",
},
],
plugins: [commonjs(), resolve(), typescript()],
plugins: [commonjs(), resolve(), typescript({ useTsconfigDeclarationDir: true })],
},
{
input: "src/index.ts",
Expand All @@ -42,6 +42,6 @@ module.exports = [
format: "iife",
}
],
plugins: [commonjs(), resolve(), typescript()],
plugins: [commonjs(), resolve(), typescript({ useTsconfigDeclarationDir: true })],
},
];
2 changes: 1 addition & 1 deletion src/create-passthrough.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export function createPassthrough(fakeXHR, nativeXMLHttpRequest) {
var evts = ['error', 'timeout', 'abort', 'readystatechange'];

// event types to handle on the xhr.upload
var uploadEvents = [];
var uploadEvents: string[] = [];

// properties to copy from the native xhr to fake xhr
var lifecycleProps = [
Expand Down
2 changes: 1 addition & 1 deletion src/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import FakeXMLHttpRequest from 'fake-xml-http-request';
import { createPassthrough } from './create-passthrough';

export function interceptor(ctx) {
function FakeRequest() {
function FakeRequest(this: any) {
Copy link
Author

@katehedgpeth katehedgpeth Oct 26, 2021

Choose a reason for hiding this comment

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

I really hate using any, but I wasn't totally sure what this should be. Is it Pretender?

Copy link
Member

Choose a reason for hiding this comment

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

This should really be rewritten into class :) Seems the generated type is fine, maybe just leave it there for now

// super()
FakeXMLHttpRequest.call(this);
}
Expand Down
51 changes: 41 additions & 10 deletions src/pretender.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import * as FakeFetch from 'whatwg-fetch';
import FakeXMLHttpRequest from 'fake-xml-http-request';
import { Params, QueryParams } from 'route-recognizer';
import { ResponseHandler, ResponseHandlerInstance } from '../index.d';
import {
Server as ServerDeprecated,
ResponseHandler,
ResponseHandlerInstance,
ResponseData as _ResponseData,
RequestHandler as _RequestHandler,
} from '../index.d';
import Hosts from './hosts';
import parseURL from './parse-url';
import Registry from './registry';
Expand All @@ -17,6 +23,10 @@ type FakeRequest = FakeXMLHttpRequest & ExtraRequestData;

type Verb = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';

export type Server = ServerDeprecated;
export type ResponseData = _ResponseData;
export type RequestHandler = _RequestHandler;

class NoopArray {
length = 0;
push(..._items: any[]) {
Expand Down Expand Up @@ -202,7 +212,7 @@ export default class Pretender {
}

handleRequest(request: FakeRequest) {
let verb = request.method.toUpperCase();
let verb = request.method.toUpperCase() as Verb;
let path = request.url;

let handler = this._handlerFor(verb, path, request);
Expand Down Expand Up @@ -244,7 +254,7 @@ export default class Pretender {
_handleRequest(result);
}
} catch (error) {
this.erroredRequest(verb, path, request, error);
this.erroredRequest(verb, path, request, error as Error);
this.resolve(request);
}
} else {
Expand Down Expand Up @@ -289,27 +299,43 @@ export default class Pretender {
}

requiresManualResolution(verb: string, path: string) {
let handler = this._handlerFor(verb.toUpperCase(), path, {});
let handler = this._handlerFor(
verb.toUpperCase() as Verb,
path,
{} as FakeRequest
);
if (!handler) {
return false;
}

let async = handler.handler.async;
return typeof async === 'function' ? async() === true : async === true;
}
prepareBody(body, _headers) {
prepareBody(body: string, _headers) {
return body;
}
prepareHeaders(headers) {
prepareHeaders(headers: { [k: string]: string }) {
return headers;
}
handledRequest(_verb, _path, _request) {
handledRequest(
_verb: string,
_path: string,
_request: FakeXMLHttpRequest & ExtraRequestData
) {
/* no-op */
}
passthroughRequest(_verb, _path, _request) {
passthroughRequest(
_verb: string,
_path: string,
_request: FakeXMLHttpRequest & ExtraRequestData
) {
/* no-op */
}
unhandledRequest(verb, path, _request) {
unhandledRequest(
verb: string,
path: string,
_request: FakeXMLHttpRequest & ExtraRequestData
) {
throw new Error(
'Pretender intercepted ' +
verb +
Expand All @@ -318,7 +344,12 @@ export default class Pretender {
' but no handler was defined for this type of request'
);
}
erroredRequest(verb, path, _request, error) {
erroredRequest(
verb: string,
path: string,
_request: FakeXMLHttpRequest & ExtraRequestData,
error: Error
) {
error.message =
'Pretender intercepted ' +
verb +
Expand Down
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"compilerOptions": {
/* Basic Options */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"module": "ESNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
"declaration": false, /* Generates corresponding '.d.ts' file. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"declarationDir": "dist",
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
Expand Down