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

API method signature refactor #78

Merged
merged 16 commits into from
Dec 20, 2017
Merged
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
2 changes: 1 addition & 1 deletion packages/arcgis-rest-auth/src/ApplicationSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Apache-2.0 */

import { IAuthenticationManager } from "@esri/arcgis-rest-request";
import { fetchToken } from "./fetchToken";
import { fetchToken } from "./fetch-token";

export interface IApplicationSessionOptions {
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/arcgis-rest-auth/src/UserSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
ArcGISRequestError,
IAuthenticationManager
} from "@esri/arcgis-rest-request";
import { generateToken } from "./generateToken";
import { fetchToken, IFetchTokenResponse } from "./fetchToken";
import { generateToken } from "./generate-token";
import { fetchToken, IFetchTokenResponse } from "./fetch-token";

/**
* Internal utility for resolving a Promise from outside its constructor.
Expand Down
18 changes: 18 additions & 0 deletions packages/arcgis-rest-auth/src/authenticated-request-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ApplicationSession } from "./ApplicationSession";
import { UserSession } from "./UserSession";

import { IRequestOptions } from "@esri/arcgis-rest-request";

/**
* Used internally by packages for requests that require user authentication.
*/
export interface IAuthenticatedRequestOptions extends IRequestOptions {
authentication: UserSession | ApplicationSession;
}

/**
* Used internally by packages for requests that require authentication.
*/
export interface IUserRequestOptions extends IRequestOptions {
authentication: UserSession;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ export interface IFetchTokenResponse {

export function fetchToken(
url: string,
params: IFetchTokenParams
options: IFetchTokenParams
): Promise<IFetchTokenResponse> {
return request(url, params).then((response: IFetchTokenRawResponse) => {
return request(url, {
params: options
}).then((response: IFetchTokenRawResponse) => {
const r: IFetchTokenResponse = {
token: response.access_token,
username: response.username,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ export function generateToken(
params.referer = "@esri.arcgis-rest-auth";
}

return request(url, params);
return request(url, { params });
}
5 changes: 3 additions & 2 deletions packages/arcgis-rest-auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./ApplicationSession";
export * from "./UserSession";
export * from "./fetchToken";
export * from "./generateToken";
export * from "./fetch-token";
export * from "./generate-token";
export * from "./authenticated-request-options";
48 changes: 18 additions & 30 deletions packages/arcgis-rest-auth/test/fetchToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ import { fetchToken } from "../src/index";
const TOKEN_URL = "https://www.arcgis.com/sharing/rest/oauth2/token";

describe("fetchToken()", () => {
let paramsSpy: jasmine.Spy;

beforeEach(() => {
paramsSpy = spyOn(FormData.prototype, "append").and.callThrough();
});

afterAll(() => {
paramsSpy.calls.reset();
});

afterEach(fetchMock.restore);

it("should request a token with `client_credentials`, `client_id` and `client_secret`", done => {
Expand All @@ -28,15 +18,14 @@ describe("fetchToken()", () => {
grant_type: "client_credentials"
})
.then(response => {
const [url]: [string, RequestInit] = fetchMock.lastCall(TOKEN_URL);
expect(url).toEqual(TOKEN_URL);
expect(paramsSpy).toHaveBeenCalledWith("f", "json");
expect(paramsSpy).toHaveBeenCalledWith("client_id", "clientId");
expect(paramsSpy).toHaveBeenCalledWith("client_secret", "clientSecret");
expect(paramsSpy).toHaveBeenCalledWith(
"grant_type",
"client_credentials"
const [url, options]: [string, RequestInit] = fetchMock.lastCall(
TOKEN_URL
);
expect(url).toEqual(TOKEN_URL);
expect(options.body).toContain("f=json");
expect(options.body).toContain("client_id=clientId");
expect(options.body).toContain("client_secret=clientSecret");
expect(options.body).toContain("grant_type=client_credentials");
expect(response.token).toEqual("token");
expect(response.expires).toBeGreaterThan(Date.now());
done();
Expand All @@ -61,20 +50,19 @@ describe("fetchToken()", () => {
grant_type: "authorization_code"
})
.then(response => {
const [url]: [string, RequestInit] = fetchMock.lastCall(TOKEN_URL);
expect(url).toEqual(TOKEN_URL);
expect(paramsSpy).toHaveBeenCalledWith("f", "json");

expect(paramsSpy).toHaveBeenCalledWith("client_id", "clientId");
expect(paramsSpy).toHaveBeenCalledWith(
"redirect_uri",
"https://example-app.com/redirect-uri"
const [url, options]: [string, RequestInit] = fetchMock.lastCall(
TOKEN_URL
);
expect(paramsSpy).toHaveBeenCalledWith(
"grant_type",
"authorization_code"
expect(url).toEqual(TOKEN_URL);
expect(options.body).toContain("f=json");
expect(options.body).toContain("client_id=clientId");
expect(options.body).toContain(
`redirect_uri=${encodeURIComponent(
"https://example-app.com/redirect-uri"
)}`
);
expect(paramsSpy).toHaveBeenCalledWith("code", "authorizationCode");
expect(options.body).toContain("grant_type=authorization_code");
expect(options.body).toContain("code=authorizationCode");
expect(response.token).toEqual("token");
expect(response.refreshToken).toEqual("refreshToken");
expect(response.username).toEqual("Casey");
Expand Down
20 changes: 6 additions & 14 deletions packages/arcgis-rest-auth/test/generateToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ import { TOMORROW } from "./utils";
const TOKEN_URL = "https://www.arcgis.com/sharing/rest/generateToken";

describe("generateToken()", () => {
let paramsSpy: jasmine.Spy;

beforeEach(() => {
paramsSpy = spyOn(FormData.prototype, "append").and.callThrough();
});

afterAll(() => {
paramsSpy.calls.reset();
});

afterEach(fetchMock.restore);

it("should generate a token for a username and password", done => {
Expand All @@ -28,11 +18,13 @@ describe("generateToken()", () => {
password: "Jones"
})
.then(response => {
const [url]: [string, RequestInit] = fetchMock.lastCall(TOKEN_URL);
const [url, options]: [string, RequestInit] = fetchMock.lastCall(
TOKEN_URL
);
expect(url).toEqual(TOKEN_URL);
expect(paramsSpy).toHaveBeenCalledWith("f", "json");
expect(paramsSpy).toHaveBeenCalledWith("username", "Casey");
expect(paramsSpy).toHaveBeenCalledWith("password", "Jones");
expect(options.body).toContain("f=json");
expect(options.body).toContain("username=Casey");
expect(options.body).toContain("password=Jones");
expect(response.token).toEqual("token");
expect(response.expires).toEqual(TOMORROW.getTime());
done();
Expand Down
Loading