diff --git a/README.md b/README.md index 0931fdf..4cd26d8 100644 --- a/README.md +++ b/README.md @@ -201,6 +201,14 @@ Gets the details of an Etch Packet. * `eid` (String) - your Etch Packet eid * `responseQuery` (String) - _optional_ A GraphQL Query compliant query to use for the data desired in the query response. Can be left out to use default. +##### getPDFInfo(options) + +Gets the details of a PDF. +* `options` (Object) - An object with the following structure: + * `variables` (Object) - An object with the following structure: + * `castEid`: The EID of an existing Cast. + * `file`: Either a binary file (see [`prepareGraphQLFile`](#preparegraphqlfilepathorstreamlikething-options)) or a base64-encoded string in the same structure we support for the `file` objects in the `createEtchPacket` mutation. See the [API Documentation](#api-documentation) for more. + ##### generateEtchSignUrl(options) Generates an Etch sign URL for an Etch Packet signer. The Etch Packet and its signers must have already been created. diff --git a/src/graphql/queries/getPDFInfo.js b/src/graphql/queries/getPDFInfo.js new file mode 100644 index 0000000..84bf5d6 --- /dev/null +++ b/src/graphql/queries/getPDFInfo.js @@ -0,0 +1,13 @@ +module.exports = { + generateQuery: () => ` + query GetPDFInfo ( + $castEid: String, + $file: Upload, + ) { + getPDFInfo ( + castEid: $castEid, + file: $file, + ) + } + `, +} diff --git a/src/index.js b/src/index.js index 7c03a8f..97eb241 100644 --- a/src/index.js +++ b/src/index.js @@ -22,6 +22,9 @@ const { etchPacket: { generateQuery: generateEtchPacketQuery, }, + getPDFInfo: { + generateQuery: generateGetPDFInfoQuery, + }, }, } = require('./graphql') @@ -136,6 +139,16 @@ class Anvil { ) } + getPDFInfo ({ variables }) { + return this.requestGraphQL( + { + query: generateGetPDFInfoQuery(), + variables, + }, + { dataType: DATA_TYPE_JSON }, + ) + } + createEtchPacket ({ variables, responseQuery, mutation }) { return this.requestGraphQL( { diff --git a/test/index.test.js b/test/index.test.js index 081bf7a..163c7eb 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -42,6 +42,12 @@ function mockNodeFetchResponse (options = {}) { } describe('Anvil API Client', function () { + const sandbox = sinon.createSandbox() + + afterEach(async function () { + sandbox.restore() + }) + describe('constructor', function () { it('throws an error when no options specified', async function () { expect(() => new Anvil()).to.throw('options are required') @@ -69,7 +75,7 @@ describe('Anvil API Client', function () { beforeEach(async function () { client = new Anvil({ apiKey: 'abc123' }) - sinon.stub(client, '_request') + sandbox.stub(client, '_request') }) describe('requestREST', function () { @@ -372,14 +378,9 @@ describe('Anvil API Client', function () { describe('requestGraphQL', function () { beforeEach(function () { - sinon.stub(client, '_wrapRequest') + sandbox.stub(client, '_wrapRequest') client._wrapRequest.callsFake(async () => ({})) - sinon.stub(client, '_request') - }) - - afterEach(function () { - client._wrapRequest.restore() - client._request.restore() + sandbox.stub(client, '_request') }) describe('without files', function () { @@ -413,11 +414,7 @@ describe('Anvil API Client', function () { describe('with files', function () { beforeEach(function () { - sinon.spy(FormData.prototype, 'append') - }) - - afterEach(function () { - FormData.prototype.append.restore() + sandbox.spy(FormData.prototype, 'append') }) describe('schema is good', function () { @@ -526,11 +523,7 @@ describe('Anvil API Client', function () { describe('createEtchPacket', function () { beforeEach(function () { - sinon.stub(client, 'requestGraphQL') - }) - - afterEach(function () { - client.requestGraphQL.restore() + sandbox.stub(client, 'requestGraphQL') }) context('mutation is specified', function () { @@ -599,14 +592,11 @@ describe('Anvil API Client', function () { describe('generateEtchSignUrl', function () { def('statusCode', 200) beforeEach(async function () { - sinon.stub(client, '_request') + sandbox.stub(client, '_request') client._request.callsFake((url, options) => { return Promise.resolve($.nodeFetchResponse) }) }) - afterEach(function () { - client._request.restore() - }) context('everything goes well', function () { def('data', { @@ -650,11 +640,7 @@ describe('Anvil API Client', function () { describe('getEtchPacket', function () { def('variables', { eid: 'etchPacketEid123' }) beforeEach(function () { - sinon.stub(client, 'requestGraphQL') - }) - - afterEach(function () { - client.requestGraphQL.restore() + sandbox.stub(client, 'requestGraphQL') }) context('no responseQuery specified', function () { @@ -694,5 +680,29 @@ describe('Anvil API Client', function () { }) }) }) + + describe('getPDFInfo', function () { + def('variables', { castEid: 'castEidAbc123456' }) + + beforeEach(function () { + sandbox.stub(client, 'requestGraphQL') + }) + + it('called getPDFInfo', async function () { + client.getPDFInfo({ variables: $.variables }) + + expect(client.requestGraphQL).to.have.been.calledOnce + const [options, clientOptions] = client.requestGraphQL.lastCall.args + + const { + query, + variables: variablesReceived, + } = options + + expect($.variables).to.eql(variablesReceived) + expect(query).to.include('getPDFInfo (') + expect(clientOptions).to.eql({ dataType: 'json' }) + }) + }) }) })