Skip to content

Commit

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

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

export interface Credentials {
clientId: string;
clientSecret: string;
}

export async function get<T>(
creds: Credentials,
userAgent: string,
path: string,
query: Query = {}
): Promise<T> {
const auth: BasicAuth = { user: creds.clientId, pass: creds.clientSecret };
return core.get(endpoint, `${path}.json`, query, userAgent, auth);
}

export async function post<T>(
creds: Credentials,
userAgent: string,
path: string,
json: Data,
query: Query = {}
): Promise<T> {
const auth: BasicAuth = { user: creds.clientId, pass: creds.clientSecret };
return core.post(endpoint, `${path}.json`, json, query, userAgent, auth);
}

export async function postForm<T>(
creds: Credentials,
userAgent: string,
path: string,
form: Data,
query: Query = {}
): Promise<T> {
const auth: BasicAuth = { user: creds.clientId, pass: creds.clientSecret };
return core.postForm(endpoint, `${path}.json`, form, query, userAgent, auth);
}
185 changes: 185 additions & 0 deletions test/helper/api/creds.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import nock from "nock";
import * as creds from "../../../src/helper/api/creds";

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

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

const c = { clientId: "cid", clientSecret: "csec" };
await creds.get(c, "baz", "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://www.reddit.com")
.get("/foo/bar.json?api_type=json&raw_json=1")
.reply(200, { bim: "bom" });

const c = { clientId: "cid", clientSecret: "csec" };
const req = creds.get(c, "baz", "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://www.reddit.com")
.get("/foo/bar.json?api_type=json&raw_json=1")
.reply(200, { error: "whoops" });

const c = { clientId: "cid", clientSecret: "csec" };
const req = creds.get(c, "baz", "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://www.reddit.com")
.get("/foo/bar.json?api_type=json&raw_json=1")
.reply(200, {
error: "whoops",
error_description: "something went wrong :(",
});

const c = { clientId: "cid", clientSecret: "csec" };
const req = creds.get(c, "baz", "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://www.reddit.com")
.post("/foo/bar.json?api_type=json&raw_json=1")
.reply(200, { bim: "bom" });

const c = { clientId: "cid", clientSecret: "csec" };
await creds.post(c, "baz", "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://www.reddit.com")
.post("/foo/bar.json?api_type=json&raw_json=1")
.reply(200, { bim: "bom" });

const c = { clientId: "cid", clientSecret: "csec" };
const req = creds.post(c, "baz", "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://www.reddit.com")
.post("/foo/bar.json?api_type=json&raw_json=1")
.reply(200, { error: "whoops" });

const c = { clientId: "cid", clientSecret: "csec" };
const req = creds.post(c, "baz", "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://www.reddit.com")
.post("/foo/bar.json?api_type=json&raw_json=1")
.reply(200, {
error: "whoops",
error_description: "something went wrong :(",
});

const c = { clientId: "cid", clientSecret: "csec" };
const req = creds.post(c, "baz", "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://www.reddit.com")
.post("/foo/bar.json?api_type=json&raw_json=1")
.reply(200, { bim: "bom" });

const c = { clientId: "cid", clientSecret: "csec" };
await creds.postForm(c, "baz", "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://www.reddit.com")
.post("/foo/bar.json?api_type=json&raw_json=1")
.reply(200, { bim: "bom" });

const c = { clientId: "cid", clientSecret: "csec" };
const req = creds.postForm(c, "baz", "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://www.reddit.com")
.post("/foo/bar.json?api_type=json&raw_json=1")
.reply(200, { error: "whoops" });

const c = { clientId: "cid", clientSecret: "csec" };
const req = creds.postForm(c, "baz", "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://www.reddit.com")
.post("/foo/bar.json?api_type=json&raw_json=1")
.reply(200, {
error: "whoops",
error_description: "something went wrong :(",
});

const c = { clientId: "cid", clientSecret: "csec" };
const req = creds.postForm(c, "baz", "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 22c7f87

Please sign in to comment.