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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@makehq/sdk",
"version": "1.19.0",
"version": "1.20.0",
"description": "Make TypeScript SDK",
"license": "MIT",
"author": "Make",
Expand Down
11 changes: 8 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import type { QueryValue, JSONValue } from './types.js';
/** Regular expression to validate a UUIDv4 string format */
const UUIDv4_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;

/** Prefix of Make API keys in the `mku_<uuid>` format. Case-sensitive, unlike the UUID that follows it. */
const MKU_API_KEY_PREFIX = 'mku_';

/**
* Check if a value is an object (not null and not an array)
* @param value Value to check
Expand All @@ -13,12 +16,14 @@ export function isObject(value: unknown): value is object {
}

/**
* Determine if a string is in API key format (UUIDv4)
* Determine if a string is in API key format: either a UUIDv4 or an `mku_` prefixed key (`mku_` + UUIDv4).
* The prefixed form accepts whatever the bare form accepts, including an uppercase UUID.
* @param value The string to check
* @returns True if the string matches UUIDv4 format used for API keys
* @returns True if the string matches one of the API key formats, which are sent with the `Token` scheme
*/
export function isAPIKey(value: string): boolean {
return UUIDv4_RE.test(value);
const uuid = value.startsWith(MKU_API_KEY_PREFIX) ? value.slice(MKU_API_KEY_PREFIX.length) : value;
return UUIDv4_RE.test(uuid);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions test/make.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ describe('Make SDK', () => {
expect(await make.users.me()).toBe(null);
});

it('should send a non-key string as Bearer', async () => {
const oauthMake = new Make('eyJhbGciOi.x.y', MAKE_ZONE);
mockFetch('GET https://make.local/api/v2/users/me', { authUser: null }, req => {
expect(req.headers.get('authorization')).toBe('Bearer eyJhbGciOi.x.y');
});

expect(await oauthMake.users.me()).toBe(null);
});

it('should send an mku_ API key with the Token scheme', async () => {
const mkuKey = 'mku_2b58802d-d95a-4715-acd5-b65acaaef470';
const mkuMake = new Make(mkuKey, MAKE_ZONE);
mockFetch('GET https://make.local/api/v2/users/me', { authUser: null }, req => {
expect(req.headers.get('authorization')).toBe(`Token ${mkuKey}`);
});

expect(await mkuMake.users.me()).toBe(null);
});

it('Should throw error when status code is 400', async () => {
mockFetch(
'GET https://make.local/api/v2/users/me',
Expand Down
36 changes: 35 additions & 1 deletion test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
import { describe, expect, it } from '@jest/globals';
import { buildUrl, createMakeError } from '../src/utils.js';
import { buildUrl, createMakeError, isAPIKey } from '../src/utils.js';

describe('Utils', () => {
describe('isAPIKey', () => {
it('should accept a UUIDv4', () => {
expect(isAPIKey('2b58802d-d95a-4715-acd5-b65acaaef470')).toBe(true);
});

it('should accept an mku_ key followed by a UUIDv4', () => {
expect(isAPIKey('mku_2b58802d-d95a-4715-acd5-b65acaaef470')).toBe(true);
});

it('should accept an mku_ key followed by an uppercase UUIDv4', () => {
expect(isAPIKey('mku_2B58802D-D95A-4715-ACD5-B65ACAAEF470')).toBe(true);
});

it.each([
['an uppercase prefix', 'MKU_2b58802d-d95a-4715-acd5-b65acaaef470'],
['an undashed 32 hex body', 'mku_2b58802dd95a4715acd5b65acaaef470'],
['the old checksum shape', 'mku_2b58802dd95a4715acd5b65acaaef4701nJqvq'],
['a non-v4 UUID', 'mku_2b58802d-d95a-1715-acd5-b65acaaef470'],
['an extra trailing character', 'mku_2b58802d-d95a-4715-acd5-b65acaaef470a'],
['no UUID', 'mku_'],
['a trailing newline', 'mku_2b58802d-d95a-4715-acd5-b65acaaef470\n'],
])('should reject an mku_ key with %s', (_label, value) => {
expect(isAPIKey(value)).toBe(false);
});

it('should reject a JWT-shaped token', () => {
expect(isAPIKey('eyJhbGciOi.x.y')).toBe(false);
});

it('should reject an empty string', () => {
expect(isAPIKey('')).toBe(false);
});
});

describe('buildUrl', () => {
it('Should build URL without parameters', () => {
expect(buildUrl('https://example.com')).toBe('https://example.com');
Expand Down