Skip to content

Commit

Permalink
working prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickarlt committed Apr 10, 2019
1 parent 71d3f2a commit d89be63
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 140 deletions.
77 changes: 40 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"rollup-plugin-filesize": "^5.0.1",
"rollup-plugin-json": "^2.3.0",
"rollup-plugin-node-resolve": "^3.0.3",
"rollup-plugin-typescript2": "^0.4.6",
"rollup-plugin-typescript": "^1.0.1",
"rollup-plugin-uglify": "^3.0.0",
"shelljs": "^0.7.8",
"slug": "^0.9.1",
Expand Down
3 changes: 2 additions & 1 deletion packages/arcgis-rest-request/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ export * from "./utils/IFetchTokenParams";
export * from "./utils/IGenerateTokenParams";
export * from "./utils/IParams";
export * from "./utils/IRequestOptions";
export * from "./utils/ITokenRequestOptions";
export * from "./utils/process-params";
export * from "./utils/ResponseFormats";
export * from "./utils/retryAuthError";
export * from "./utils/warn";
// export * from "./utils/with-request-options";
export * from "./utils/with-options";
export * from "./utils/with-url";
63 changes: 50 additions & 13 deletions packages/arcgis-rest-request/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,33 @@ import { IParams } from "./utils/IParams";

export const NODEJS_DEFAULT_REFERER_HEADER = `@esri/arcgis-rest-js`;

export const DEFAULT_ARCGIS_REQUEST_OPTIONS: Pick<
IRequestOptions,
Exclude<keyof IRequestOptions, "url">
> = {
httpMethod: "POST",
params: {
f: "json"
}
};

function mergeOptions(options: IRequestOptions): IRequestOptions {
const params = {
...DEFAULT_ARCGIS_REQUEST_OPTIONS.params,
...options.params
};
const headers = {
...DEFAULT_ARCGIS_REQUEST_OPTIONS.headers,
...options.headers
};

return {
...DEFAULT_ARCGIS_REQUEST_OPTIONS,
...options,
...{ params, headers }
};
}

/**
* ```js
* import { request } from '@esri/arcgis-rest-request';
Expand All @@ -34,24 +61,34 @@ export const NODEJS_DEFAULT_REFERER_HEADER = `@esri/arcgis-rest-js`;
* @returns A Promise that will resolve with the data from the response.
*/
export function request(url: string | IRequestOptions): Promise<any>;
export function request(
requestUrl: string,
requestOptions: IRequestOptions
): Promise<any>;
export function request(url: string, options?: IRequestOptions): Promise<any>;
export function request(
requestUrl: string | IRequestOptions,
requestOptions?: IRequestOptions
): Promise<any> {
let url = typeof requestUrl === "string" ? requestUrl : requestOptions.url;
const options: IRequestOptions = {
httpMethod: "POST",
...{ params: { f: "json" } },
...(typeof url === "string" ? requestOptions : url)
};

const missingGlobals: string[] = [];
const recommendedPackages: string[] = [];

let options: IRequestOptions;
let url: string;

if (typeof requestUrl === "string") {
url = requestUrl;
if (requestOptions) {
options = mergeOptions(requestOptions);
} else {
options = mergeOptions({});
}
} else {
url = requestUrl.url;
options = mergeOptions(requestUrl);
}

// @TODO discuss this!
// if (options.url) {
// url = options.url;
// }

// don't check for a global fetch if a custom implementation was passed through
if (!options.fetch && typeof fetch !== "undefined") {
options.fetch = fetch.bind(Function("return this")());
Expand Down Expand Up @@ -88,7 +125,7 @@ export function request(

const params: IParams = {
...{ f: "json" },
...requestOptions.params
...(options && options.params ? options.params : {})
};

const fetchOptions: RequestInit = {
Expand Down Expand Up @@ -144,7 +181,7 @@ export function request(

// Mixin headers from request options
fetchOptions.headers = {
...requestOptions.headers
...options.headers
};

/* istanbul ignore next - karma reports coverage on browser tests only */
Expand Down
3 changes: 2 additions & 1 deletion packages/arcgis-rest-request/src/utils/retryAuthError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { IRequestOptions, IAuthenticationManager } from "../request";
import { IRequestOptions } from "./IRequestOptions";
import { IAuthenticationManager } from "./IAuthenticationManager";

export type IRetryAuthError = (
url: string,
Expand Down

This file was deleted.

23 changes: 14 additions & 9 deletions packages/arcgis-rest-request/src/utils/with-url.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { IRequestOptions } from "./IRequestOptions";

export function withUrl<T1, T2 extends (...args: any[]) => any>(
url: string,
func: T2
): (o: Parameters<T2>[0]) => ReturnType<T2> {
return (options: Parameters<T2>[0]): ReturnType<T2> => {
return func({
...options,
...{ url }
});
export function withUrl<T extends (...args: any[]) => any>(
func: T,
url: string
): (...funcArgs: Parameters<T>) => ReturnType<T> {
return (...args: Parameters<T>): ReturnType<T> => {
const options: IRequestOptions =
typeof args[args.length - 1] === "object"
? {
...{ url },
...args.pop()
}
: { url };

return func(...[...args, options]);
};
}
31 changes: 30 additions & 1 deletion packages/arcgis-rest-request/test/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { request, ErrorTypes } from "../src/index";
import {
request,
ErrorTypes,
DEFAULT_ARCGIS_REQUEST_OPTIONS
} from "../src/index";
import * as fetchMock from "fetch-mock";
import {
SharingRestInfo,
Expand Down Expand Up @@ -260,6 +264,31 @@ describe("request()", () => {
});
});

it("should allow setting defaults for all requests", done => {
fetchMock.once("*", SharingRestInfo);

DEFAULT_ARCGIS_REQUEST_OPTIONS.headers = {
"Test-Header": "Test"
};

request("https://www.arcgis.com/sharing/rest/info")
.then(response => {
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual("https://www.arcgis.com/sharing/rest/info");
expect(options.method).toBe("POST");
expect(response).toEqual(SharingRestInfo);
expect(options.body).toContain("f=json");
expect((options.headers as any)["Test-Header"]).toBe("Test");
done();
})
.catch(e => {
fail(e);
});

// since calling request is sync we can delete this right away
delete DEFAULT_ARCGIS_REQUEST_OPTIONS.headers;
});

describe("should throw errors when required dependencies are missing", () => {
const oldPromise = Promise;
const oldFetch = fetch;
Expand Down
73 changes: 0 additions & 73 deletions packages/arcgis-rest-request/test/utils/with-url.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion umd-base-profile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import typescript from "rollup-plugin-typescript2";
import typescript from "rollup-plugin-typescript";
import resolve from "rollup-plugin-node-resolve";
import commonjs from "rollup-plugin-commonjs";
import json from "rollup-plugin-json";
Expand Down

0 comments on commit d89be63

Please sign in to comment.