Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ logs
.eslintcache

# Optional REPL history
.node_repl_history
.node_repl_history

# Test scripts
scripts/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ MIT License - See LICENSE file for details.
| PUT /v1/console/card-templates/{id} | `console.updateTemplate()` | Y |
| GET /v1/console/card-templates/{id} | `console.readTemplate()` | Y |
| GET .../logs | `console.eventLog()` | Y |
| GET /v1/console/pass-template-pairs | `console.listPassTemplatePairs()` | Y |
| GET /v1/console/card-template-pairs | `console.listPassTemplatePairs()` | Y |
| POST /v1/console/card-template-pairs | `console.createPassTemplatePair()` | Y |
| GET /v1/console/ledger-items | `console.ledgerItems()` | Y |
| POST .../ios_preflight | `console.iosPreflight()` | Y |
| GET /v1/console/webhooks | `console.webhooks.list()` | Y |
Expand Down
55 changes: 44 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class BaseApi {
let resourceId = null;
if (
method === "GET" ||
method === "DELETE" ||
(method === "POST" &&
(!options.body || Object.keys(options.body).length === 0))
) {
Expand All @@ -193,7 +194,11 @@ class BaseApi {
let payload;
let sigPayload;

if ((method === "POST" && !options.body) || method === "GET") {
if (
(method === "POST" && !options.body) ||
method === "GET" ||
method === "DELETE"
) {
// For these requests, use {"id": "card_id"} as the payload for signature generation
if (resourceId) {
sigPayload = JSON.stringify({ id: resourceId });
Expand Down Expand Up @@ -221,7 +226,11 @@ class BaseApi {

// Handle query parameters for GET requests or POST with empty body
let finalUrl = url;
if (method === "GET" || (method === "POST" && !options.body)) {
if (
method === "GET" ||
method === "DELETE" ||
(method === "POST" && !options.body)
) {
if (resourceId) {
// Add sig_payload to query params
const separator = finalUrl.includes("?") ? "&" : "?";
Expand All @@ -236,7 +245,11 @@ class BaseApi {
body: method !== "GET" ? payload : undefined,
});

const data = await response.json();
// Handle empty responses (204 No Content)
let data = {};
if (response.status !== 204) {
data = await response.json();
}

if (!response.ok) {
if (response.status === 401) {
Expand Down Expand Up @@ -487,6 +500,7 @@ class ConsoleApi extends BaseApi {
supportEmail: "support_email",
privacyPolicyUrl: "privacy_policy_url",
termsAndConditionsUrl: "terms_and_conditions_url",
logo: "logo",
metadata: "metadata",
};

Expand Down Expand Up @@ -560,10 +574,15 @@ class ConsoleApi extends BaseApi {

return {
provisioningCredentialIdentifier:
response.provisioningCredentialIdentifier ||
response.provisioning_credential_identifier,
sharingInstanceIdentifier: response.sharing_instance_identifier,
cardTemplateIdentifier: response.card_template_identifier,
environmentIdentifier: response.environment_identifier,
sharingInstanceIdentifier:
response.sharingInstanceIdentifier ||
response.sharing_instance_identifier,
cardTemplateIdentifier:
response.cardTemplateIdentifier || response.card_template_identifier,
environmentIdentifier:
response.environmentIdentifier || response.environment_identifier,
};
}

Expand Down Expand Up @@ -600,21 +619,35 @@ class ConsoleApi extends BaseApi {

const queryString = queryParams.toString();
const path = queryString
? `/v1/console/pass-template-pairs?${queryString}`
: "/v1/console/pass-template-pairs";
? `/v1/console/card-template-pairs?${queryString}`
: "/v1/console/card-template-pairs";

const response = await this.request(path);

if (response.pass_template_pairs) {
response.passTemplatePairs = response.pass_template_pairs.map(
if (response.card_template_pairs) {
response.passTemplatePairs = response.card_template_pairs.map(
(pair) => new PassTemplatePair(pair),
);
delete response.pass_template_pairs;
delete response.card_template_pairs;
}

return response;
}

async createPassTemplatePair(params) {
const body = {
name: params.name,
apple_card_template_id: params.appleCardTemplateId,
google_card_template_id: params.googleCardTemplateId,
};

const response = await this.request("/v1/console/card-template-pairs", {
method: "POST",
body,
});
return new PassTemplatePair(response);
}

async listLandingPages() {
const response = await this.request("/v1/console/landing-pages");
const pages = Array.isArray(response) ? response : [];
Expand Down
8 changes: 4 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ describe('AccessGrid SDK', () => {

describe('listPassTemplatePairs', () => {
const mockPairsResponse = {
pass_template_pairs: [
card_template_pairs: [
{
id: 'pair-1',
name: 'Employee Badge Pair',
Expand All @@ -577,7 +577,7 @@ describe('AccessGrid SDK', () => {
await client.console.listPassTemplatePairs();

expect(fetch).toHaveBeenCalledWith(
expect.stringContaining('/v1/console/pass-template-pairs'),
expect.stringContaining('/v1/console/card-template-pairs'),
expect.objectContaining({
method: 'GET'
})
Expand Down Expand Up @@ -611,7 +611,7 @@ describe('AccessGrid SDK', () => {
await client.console.listPassTemplatePairs();

const calledUrl = fetch.mock.calls[0][0];
expect(calledUrl).toMatch(/\/pass-template-pairs(\?sig_payload=|$)/);
expect(calledUrl).toMatch(/\/card-template-pairs(\?sig_payload=|$)/);
});

test('should return PassTemplatePair instances', async () => {
Expand All @@ -635,7 +635,7 @@ describe('AccessGrid SDK', () => {

const result = await client.console.listPassTemplatePairs();

expect(result.pass_template_pairs).toBeUndefined();
expect(result.card_template_pairs).toBeUndefined();
});

test('should deserialize nested TemplateInfo models', async () => {
Expand Down
Loading