diff --git a/package.json b/package.json index c4ca6339..1a017744 100644 --- a/package.json +++ b/package.json @@ -25,9 +25,11 @@ "@types/chai": "^4", "@types/mocha": "^5", "@types/node": "^10", + "@types/node-fetch": "^2.5.0", "chai": "^4", "globby": "^10", "mocha": "^5", + "nock": "^10.0.6", "nyc": "^13", "ts-node": "^8", "tslint": "^5", diff --git a/src/commands/identify.ts b/src/commands/identify.ts new file mode 100644 index 00000000..91eceef1 --- /dev/null +++ b/src/commands/identify.ts @@ -0,0 +1,49 @@ +import { Command, flags } from "@oclif/command" +import Gravity from "../lib/gravity" + +export default class Identify extends Command { + static description = "Identify a Gravity resource by its BSON ID" + + static flags = { + help: flags.help({ char: "h" }), + } + + static args = [{ name: "id" }] + + static collectionsToCheck: CollectionMapping[] = [ + { name: "Artist", endpoint: "artist" }, + { name: "Artwork", endpoint: "artwork" }, + { name: "Partner", endpoint: "partner" }, + ] + + async run() { + const gravity = new Gravity() + const { args } = this.parse(Identify) + const { id } = args + + const gravityPromises = Identify.collectionsToCheck.map(collection => { + const resource = `${collection.endpoint}/${id}` + return gravity.get(resource) + }) + + const gravityResponses = await Promise.all(gravityPromises) + const foundIndex = gravityResponses.findIndex(r => r.status === 200) + + if (foundIndex >= 0) { + const foundCollection = Identify.collectionsToCheck[foundIndex] + const foundResource = `${foundCollection.endpoint}/${id}` + this.log(`${foundCollection.name} ${gravity.url(`api/v1/${foundResource}`)}`) + } else { + const collections = Identify.collectionsToCheck.map(c => c.name) + this.log(`Nothing found in: ${collections.join(", ")}`) + } + } +} + +interface CollectionMapping { + /** Name of the Gravity resource */ + name: string + + /** The name of the collection as it appears in the GET endpoint for the resource, i.e. /api/v1//:id */ + endpoint: string +} diff --git a/test/commands/identify.test.ts b/test/commands/identify.test.ts new file mode 100644 index 00000000..bb8fbca5 --- /dev/null +++ b/test/commands/identify.test.ts @@ -0,0 +1,83 @@ +import { expect, test } from "@oclif/test" + +describe("identify", () => { + describe("when the artwork exists", () => { + test + .nock("https://stagingapi.artsy.net", api => + api + .get("/api/v1/artist/abc123") + .reply(404) + .get("/api/v1/artwork/abc123") + .reply(200) + .get("/api/v1/partner/abc123") + .reply(404) + ) + .stdout() + .command(["identify", "abc123"]) + .it("displays a found artwork message", ctx => { + expect(ctx.stdout).to.equal( + "Artwork https://stagingapi.artsy.net/api/v1/artwork/abc123\n" + ) + }) + }) + + describe("when the artist exists", () => { + test + .nock("https://stagingapi.artsy.net", api => + api + .get("/api/v1/artist/abc123") + .reply(200) + .get("/api/v1/artwork/abc123") + .reply(404) + .get("/api/v1/partner/abc123") + .reply(404) + ) + .stdout() + .command(["identify", "abc123"]) + .it("displays a found artist message", ctx => { + expect(ctx.stdout).to.equal( + "Artist https://stagingapi.artsy.net/api/v1/artist/abc123\n" + ) + }) + }) + + describe("when the partner exists", () => { + test + .nock("https://stagingapi.artsy.net", api => + api + .get("/api/v1/artist/abc123") + .reply(404) + .get("/api/v1/artwork/abc123") + .reply(404) + .get("/api/v1/partner/abc123") + .reply(200) + ) + .stdout() + .command(["identify", "abc123"]) + .it("displays a found partner message", ctx => { + expect(ctx.stdout).to.equal( + "Partner https://stagingapi.artsy.net/api/v1/partner/abc123\n" + ) + }) + }) + + describe("when nothing is found", () => { + test + .nock("https://stagingapi.artsy.net", api => + api + .get("/api/v1/artwork/abc123") + .reply(404) + .get("/api/v1/artist/abc123") + .reply(404) + .get("/api/v1/partner/abc123") + .reply(404) + ) + .stdout() + .command(["identify", "abc123"]) + .it("displays a not-found message", ctx => { + expect(ctx.stdout).to.equal( + "Nothing found in: Artist, Artwork, Partner\n" + ) + }) + }) +}) diff --git a/tslint.json b/tslint.json index 79f93374..88a1d9d0 100644 --- a/tslint.json +++ b/tslint.json @@ -16,6 +16,7 @@ "trailing-comma": false, "variable-name": [true, "ban-keywords", "allow-leading-underscore"], "jsdoc-format": false, - "radix": false + "radix": false, + "no-single-line-block-comment": false } } diff --git a/yarn.lock b/yarn.lock index e0b5cac4..07242b45 100644 --- a/yarn.lock +++ b/yarn.lock @@ -432,7 +432,7 @@ cardinal@^2.1.1: ansicolors "~0.3.2" redeyed "~2.1.0" -chai@^4: +chai@^4, chai@^4.1.2: version "4.2.0" resolved "https://registry.yarnpkg.com/chai/-/chai-4.2.0.tgz#760aa72cf20e3795e84b12877ce0e83737aa29e5" integrity sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw== @@ -613,6 +613,11 @@ deep-eql@^3.0.1: dependencies: type-detect "^4.0.0" +deep-equal@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5" + integrity sha1-9dJgKStmDghO/0zbyfCK0yR0SLU= + default-require-extensions@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/default-require-extensions/-/default-require-extensions-2.0.0.tgz#f5f8fbb18a7d6d50b21f641f649ebb522cfe24f7" @@ -1181,6 +1186,11 @@ json-parse-better-errors@^1.0.1: resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9" integrity sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw== +json-stringify-safe@^5.0.1: + version "5.0.1" + resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb" + integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus= + jsonfile@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb" @@ -1260,7 +1270,7 @@ lodash.templatesettings@^4.0.0: dependencies: lodash._reinterpolate "^3.0.0" -lodash@^4.17.11, lodash@^4.17.13: +lodash@^4.17.11, lodash@^4.17.13, lodash@^4.17.5: version "4.17.15" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548" integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A== @@ -1419,6 +1429,21 @@ nice-try@^1.0.4: resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ== +nock@^10.0.6: + version "10.0.6" + resolved "https://registry.yarnpkg.com/nock/-/nock-10.0.6.tgz#e6d90ee7a68b8cfc2ab7f6127e7d99aa7d13d111" + integrity sha512-b47OWj1qf/LqSQYnmokNWM8D88KvUl2y7jT0567NB3ZBAZFz2bWp2PC81Xn7u8F2/vJxzkzNZybnemeFa7AZ2w== + dependencies: + chai "^4.1.2" + debug "^4.1.0" + deep-equal "^1.0.0" + json-stringify-safe "^5.0.1" + lodash "^4.17.5" + mkdirp "^0.5.0" + propagate "^1.0.0" + qs "^6.5.1" + semver "^5.5.0" + node-fetch@^2.6.0: version "2.6.0" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd" @@ -1653,6 +1678,11 @@ pkg-dir@^4.2.0: dependencies: find-up "^4.0.0" +propagate@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/propagate/-/propagate-1.0.0.tgz#00c2daeedda20e87e3782b344adba1cddd6ad709" + integrity sha1-AMLa7t2iDofjeCs0Stuhzd1q1wk= + pseudomap@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3" @@ -1685,6 +1715,11 @@ qqjs@^0.3.10: tmp "^0.1.0" write-json-file "^4.1.1" +qs@^6.5.1: + version "6.7.0" + resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc" + integrity sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ== + read-pkg-up@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-4.0.0.tgz#1b221c6088ba7799601c808f91161c66e58f8978"