forked from thislooksfun/snoots
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add client-authorized api wrapper
- Loading branch information
1 parent
052c592
commit 22c7f87
Showing
2 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |