Skip to content

Commit

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

const endpoint = "https://www.reddit.com";
export async function get<T>(
userAgent: string,
path: string,
query: Query = {}
): Promise<T> {
return core.get(endpoint, `${path}.json`, query, userAgent);
}

export async function post<T>(
userAgent: string,
path: string,
json: Data,
query: Query = {}
): Promise<T> {
return core.post(endpoint, `${path}.json`, json, query, userAgent);
}

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

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" });

await anon.get("baz", "foo/bar", {});

// TODO: Add tests for user agent.
// 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 req = anon.get("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 req = anon.get("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 req = anon.get("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" });

await anon.post("baz", "foo/bar", { bar: "foo" }, {});

// TODO: Add tests for body and user agent.
// 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 req = anon.post("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 req = anon.post("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 req = anon.post("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" });

await anon.postForm("baz", "foo/bar", { bar: "foo" }, {});

// TODO: Add tests for body and user agent.
// 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 req = anon.postForm("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 req = anon.postForm("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 req = anon.postForm("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 052c592

Please sign in to comment.