Skip to content

Commit

Permalink
feat: add oauth api wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Mar 11, 2021
1 parent 126fecf commit 0b6501e
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/helper/api/oauth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { Data } from "../types";
import type { BearerAuth, Query } from "./core";
import * as core from "./core";

const endpoint = "https://oauth.reddit.com";

export interface OauthOpts {
token: string;
userAgent: string;
}

export async function get<T>(
opts: OauthOpts,
path: string,
query: Query = {}
): Promise<T> {
const auth: BearerAuth = { bearer: opts.token };
return core.get(endpoint, path, query, opts.userAgent, auth);
}

export async function post<T>(
opts: OauthOpts,
path: string,
json: Data,
query: Query = {}
): Promise<T> {
const auth: BearerAuth = { bearer: opts.token };
return core.post(endpoint, path, json, query, opts.userAgent, auth);
}

export async function postForm<T>(
opts: OauthOpts,
path: string,
form: Data,
query: Query = {}
): Promise<T> {
const auth: BearerAuth = { bearer: opts.token };
return core.postForm(endpoint, path, form, query, opts.userAgent, auth);
}
185 changes: 185 additions & 0 deletions test/helper/api/oauth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import nock from "nock";
import * as oauth from "../../../src/helper/api/oauth";

afterEach(() => nock.cleanAll());
afterAll(() => nock.restore());

describe("get()", () => {
it("should pass common values", async () => {
const n = nock("https://oauth.reddit.com")
.get("/foo/bar?api_type=json&raw_json=1")
.reply(200, { bim: "bom" });

const oo = { token: "sometkn", userAgent: "baz" };
await oauth.get(oo, "foo/bar", {});

// TODO: Add tests for user agent and auth.
// BODY: Pending https://github.com/nock/nock/issues/2171.
n.done();
});

it("should give back json data", async () => {
const n = nock("https://oauth.reddit.com")
.get("/foo/bar?api_type=json&raw_json=1")
.reply(200, { bim: "bom" });

const oo = { token: "sometkn", userAgent: "baz" };
const req = oauth.get(oo, "foo/bar", {});
await expect(req).resolves.toStrictEqual({ bim: "bom" });

n.done();
});

describe("when given an api error", () => {
it("should throw", async () => {
const n = nock("https://oauth.reddit.com")
.get("/foo/bar?api_type=json&raw_json=1")
.reply(200, { error: "whoops" });

const oo = { token: "sometkn", userAgent: "baz" };
const req = oauth.get(oo, "foo/bar", {});
const err = new Error("Reddit returned an error: whoops");
await expect(req).rejects.toStrictEqual(err);

n.done();
});

it("should use the description if available", async () => {
const n = nock("https://oauth.reddit.com")
.get("/foo/bar?api_type=json&raw_json=1")
.reply(200, {
error: "whoops",
error_description: "something went wrong :(",
});

const oo = { token: "sometkn", userAgent: "baz" };
const req = oauth.get(oo, "foo/bar", {});
const err = new Error(
"Reddit returned an error: whoops: something went wrong :("
);
await expect(req).rejects.toStrictEqual(err);

n.done();
});
});
});

describe("post()", () => {
it("should pass common values", async () => {
const n = nock("https://oauth.reddit.com")
.post("/foo/bar?api_type=json&raw_json=1")
.reply(200, { bim: "bom" });

const oo = { token: "sometkn", userAgent: "baz" };
await oauth.post(oo, "foo/bar", { bar: "foo" }, {});

// TODO: Add tests for body, user agent, and auth.
// BODY: Pending https://github.com/nock/nock/issues/2171.
n.done();
});

it("should give back json data", async () => {
const n = nock("https://oauth.reddit.com")
.post("/foo/bar?api_type=json&raw_json=1")
.reply(200, { bim: "bom" });

const oo = { token: "sometkn", userAgent: "baz" };
const req = oauth.post(oo, "foo/bar", { bar: "foo" }, {});
await expect(req).resolves.toStrictEqual({ bim: "bom" });

n.done();
});

describe("when given an api error", () => {
it("should throw", async () => {
const n = nock("https://oauth.reddit.com")
.post("/foo/bar?api_type=json&raw_json=1")
.reply(200, { error: "whoops" });

const oo = { token: "sometkn", userAgent: "baz" };
const req = oauth.post(oo, "foo/bar", { bar: "foo" }, {});
const err = new Error("Reddit returned an error: whoops");
await expect(req).rejects.toStrictEqual(err);

n.done();
});

it("should use the description if available", async () => {
const n = nock("https://oauth.reddit.com")
.post("/foo/bar?api_type=json&raw_json=1")
.reply(200, {
error: "whoops",
error_description: "something went wrong :(",
});

const oo = { token: "sometkn", userAgent: "baz" };
const req = oauth.post(oo, "foo/bar", { bar: "foo" }, {});
const err = new Error(
"Reddit returned an error: whoops: something went wrong :("
);
await expect(req).rejects.toStrictEqual(err);

n.done();
});
});
});

describe("postForm()", () => {
it("should pass common values", async () => {
const n = nock("https://oauth.reddit.com")
.post("/foo/bar?api_type=json&raw_json=1")
.reply(200, { bim: "bom" });

const oo = { token: "sometkn", userAgent: "baz" };
await oauth.postForm(oo, "foo/bar", { bar: "foo" }, {});

// TODO: Add tests for body, user agent, and auth.
// BODY: Pending https://github.com/nock/nock/issues/2171.
n.done();
});

it("should give back json data", async () => {
const n = nock("https://oauth.reddit.com")
.post("/foo/bar?api_type=json&raw_json=1")
.reply(200, { bim: "bom" });

const oo = { token: "sometkn", userAgent: "baz" };
const req = oauth.postForm(oo, "foo/bar", { bar: "foo" }, {});
await expect(req).resolves.toStrictEqual({ bim: "bom" });

n.done();
});

describe("when given an api error", () => {
it("should throw", async () => {
const n = nock("https://oauth.reddit.com")
.post("/foo/bar?api_type=json&raw_json=1")
.reply(200, { error: "whoops" });

const oo = { token: "sometkn", userAgent: "baz" };
const req = oauth.postForm(oo, "foo/bar", { bar: "foo" }, {});
const err = new Error("Reddit returned an error: whoops");
await expect(req).rejects.toStrictEqual(err);

n.done();
});

it("should use the description if available", async () => {
const n = nock("https://oauth.reddit.com")
.post("/foo/bar?api_type=json&raw_json=1")
.reply(200, {
error: "whoops",
error_description: "something went wrong :(",
});

const oo = { token: "sometkn", userAgent: "baz" };
const req = oauth.postForm(oo, "foo/bar", { bar: "foo" }, {});
const err = new Error(
"Reddit returned an error: whoops: something went wrong :("
);
await expect(req).rejects.toStrictEqual(err);

n.done();
});
});
});

0 comments on commit 0b6501e

Please sign in to comment.