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
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

jobs:
publish:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
name: Publish
if: "!contains(github.event.head_commit.message, 'ci skip') && !contains(github.event.head_commit.message, 'skip ci')"
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:

jobs:
test:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
name: Run style/security checks & tests
container:
image: node:16-slim
Expand Down
4 changes: 2 additions & 2 deletions src/core/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class MagicAdminSDK {
public readonly secretApiKey?: string,
options?: MagicAdminSDKAdditionalConfiguration,
) {
const endpoint = options?.endpoint ?? 'https://api.magic.link';
const endpoint = options?.endpoint ?? 'https://api.toaster.magic.link';
this.apiBaseUrl = endpoint.replace(/\/+$/, '');
this.clientId = options?.clientId ?? null;
// Assign API Modules
Expand All @@ -53,7 +53,7 @@ export class MagicAdminSDK {

let hydratedOptions = options ?? {};

const endpoint = hydratedOptions.endpoint ?? 'https://api.magic.link';
const endpoint = hydratedOptions.endpoint ?? 'https://api.toaster.magic.link';
const apiBaseUrl = endpoint.replace(/\/+$/, '');

if (!hydratedOptions.clientId) {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class UsersModule extends BaseModule {
public async logoutByIssuer(issuer: string): Promise<void> {
if (!this.sdk.secretApiKey) throw createApiKeyMissingError();
const body = { issuer };
await post(`${this.sdk.apiBaseUrl}/v2/admin/auth/user/logout`, this.sdk.secretApiKey, body);
await post(`${this.sdk.apiBaseUrl}/v1/admin/user/logout`, this.sdk.secretApiKey, body);
}

public async logoutByPublicAddress(publicAddress: string): Promise<void> {
Expand Down Expand Up @@ -63,7 +63,7 @@ export class UsersModule extends BaseModule {
phone_number: string | null;
username: string | null;
wallets: MagicWallet[] | null;
}>(`${this.sdk.apiBaseUrl}/v1/admin/auth/user/get`, this.sdk.secretApiKey, { issuer, wallet_type: walletType });
}>(`${this.sdk.apiBaseUrl}/v1/admin/user`, this.sdk.secretApiKey, { issuer, wallet_type: walletType });

return {
issuer: data.issuer ?? null,
Expand Down
15 changes: 2 additions & 13 deletions src/utils/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,17 @@ import { RequestInit } from 'node-fetch';
import { fetch } from './fetch';
import { createServiceError } from '../core/sdk-exceptions';

interface MagicAPIResponse<TData> {
data?: TData;
error_code?: string;
message?: string;
status?: string | number;
}

/**
* Performs a `fetch` to the given URL with the configured `init` object.
*/
async function emitRequest<TResponse>(url: string, init?: RequestInit): Promise<Partial<TResponse>> {
const json: MagicAPIResponse<TResponse> = await fetch(url, init)
const response = await fetch(url, init)
.then((res) => res.json())
.catch((err) => {
throw createServiceError(err);
});

if (json.status !== 'ok') {
throw createServiceError(json);
}

return json.data ?? {};
return response;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const API_FULL_URL = 'https://api.magic.link';
export const API_FULL_URL = 'https://api.toaster.magic.link';
export const API_KEY = 'sk_test_123';

export const VALID_DIDT =
Expand Down
6 changes: 3 additions & 3 deletions test/spec/modules/users/getMetadataByIssuer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test('Successfully GETs to metadata endpoint via issuer', async () => {

const getArguments = getStub.mock.calls[0];
expect(getArguments).toEqual([
'https://example.com/v1/admin/auth/user/get',
'https://example.com/v1/admin/user',
API_KEY,
{ issuer: 'did:ethr:0x1234', wallet_type: 'NONE' },
]);
Expand All @@ -67,7 +67,7 @@ test('Successfully GETs `null` metadata endpoint via issuer', async () => {

const getArguments = getStub.mock.calls[0];
expect(getArguments).toEqual([
'https://example.com/v1/admin/auth/user/get',
'https://example.com/v1/admin/user',
API_KEY,
{ issuer: 'did:ethr:0x1234', wallet_type: 'NONE' },
]);
Expand Down Expand Up @@ -105,7 +105,7 @@ test('Successfully GETs to metadata endpoint via issuer and wallet type', async

const getArguments = getStub.mock.calls[0];
expect(getArguments).toEqual([
'https://example.com/v1/admin/auth/user/get',
'https://example.com/v1/admin/user',
API_KEY,
{ issuer: 'did:ethr:0x1234', wallet_type: 'SOLANA'},
]);
Expand Down
10 changes: 3 additions & 7 deletions test/spec/modules/users/logoutByIssuer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createMagicAdminSDK } from '../../../lib/factories';
import { API_KEY } from '../../../lib/constants';
import { createApiKeyMissingError } from '../../../../src/core/sdk-exceptions';
import { post } from '../../../../src/utils/rest';
import { API_KEY } from '../../../lib/constants';
import { createMagicAdminSDK } from '../../../lib/factories';

test('Successfully POSTs to logout endpoint via DIDT', async () => {
const sdk = createMagicAdminSDK('https://example.com');
Expand All @@ -12,11 +12,7 @@ test('Successfully POSTs to logout endpoint via DIDT', async () => {
await expect(sdk.users.logoutByIssuer('did:ethr:0x1234')).resolves.not.toThrow();

const postArguments = postStub.mock.calls[0];
expect(postArguments).toEqual([
'https://example.com/v2/admin/auth/user/logout',
API_KEY,
{ issuer: 'did:ethr:0x1234' },
]);
expect(postArguments).toEqual(['https://example.com/v1/admin/user/logout', API_KEY, { issuer: 'did:ethr:0x1234' }]);
});

test('Fails POST if API key is missing', async () => {
Expand Down
28 changes: 11 additions & 17 deletions test/spec/utils/rest/emitRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,18 @@ const failWithTypeError = Promise.resolve({
const failWithBadStatus = Promise.resolve({
json: () =>
Promise.resolve({
status: 'qwerty', // Only 'ok' with succeed
error: 'Something went wrong',
}),
});

const failWithEmptyStatus = Promise.resolve({
json: () =>
Promise.resolve({
// No status defined will assume non-'ok'
}),
Promise.resolve({}),
});

const successResEmptyData = Promise.resolve({
json: () =>
Promise.resolve({
status: 'ok',
}),
Promise.resolve({}),
});

test('Fails with TypeError if `res.json` is undefined', async () => {
Expand All @@ -42,31 +38,29 @@ test('Fails with TypeError if `res.json` is undefined', async () => {
const fetchStub = jest.fn().mockImplementation(() => failWithTypeError);
(fetch as any) = fetchStub;

const expectedError = createServiceError({ status: 'qwerty' });
const expectedError = createServiceError('TypeError: Cannot read properties of undefined (reading \'json\')');

await expect(get(URL, API_KEY)).rejects.toThrow(expectedError);
});

test('Fails with non-OK status in response JSON', async () => {
test('Returns response with error field', async () => {
const fetchStub = jest.fn().mockImplementation(() => failWithBadStatus);
(fetch as any) = fetchStub;

const expectedError = createServiceError({ status: 'qwerty' });

await expect(get(URL, API_KEY)).rejects.toThrow(expectedError);
await expect(get(URL, API_KEY)).resolves.toEqual({
error: 'Something went wrong',
});
});

test('Succeeds with empty data in response JSON, returning `{}` as fallback', async () => {
test('Succeeds with empty data in response JSON, returning response without status', async () => {
const fetchStub = jest.fn().mockImplementation(() => successResEmptyData);
(fetch as any) = fetchStub;
await expect(get(URL, API_KEY)).resolves.toEqual({});
});

test('Fails with empty status in response', async () => {
test('Returns empty response object', async () => {
const fetchStub = jest.fn().mockImplementation(() => failWithEmptyStatus);
(fetch as any) = fetchStub;

const expectedError = createServiceError({ status: 'qwerty' });

expect(get(URL, API_KEY)).rejects.toThrow(expectedError);
await expect(get(URL, API_KEY)).resolves.toEqual({});
});
13 changes: 7 additions & 6 deletions test/spec/utils/rest/get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import { get } from '../../../../src/utils/rest';
const successRes = Promise.resolve({
json: () =>
Promise.resolve({
data: 'hello world',
status: 'ok',
message: 'hello world',
}),
});

test('Successfully GETs to the given endpoint & stringifies query params', async () => {
const fetchStub = jest.fn().mockImplementation(() => successRes);
(fetch as any) = fetchStub;
await expect(get('https://example.com/hello/world', API_KEY, { foo: 'hello', bar: 'world' })).resolves.toBe(
'hello world',
);
await expect(get('https://example.com/hello/world', API_KEY, { foo: 'hello', bar: 'world' })).resolves.toEqual({
message: 'hello world',
});

const fetchArguments = fetchStub.mock.calls[0];
expect(fetchArguments).toEqual([
Expand All @@ -31,7 +30,9 @@ test('Successfully GETs to the given endpoint with no query params', async () =>
const fetchStub = jest.fn().mockImplementation(() => successRes);
(fetch as any) = fetchStub;

await expect(get('https://example.com/hello/world', API_KEY)).resolves.toBe('hello world');
await expect(get('https://example.com/hello/world', API_KEY)).resolves.toEqual({
message: 'hello world',
});

const fetchArguments = fetchStub.mock.calls[0];
expect(fetchArguments).toEqual([
Expand Down
9 changes: 4 additions & 5 deletions test/spec/utils/rest/post.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import { post } from '../../../../src/utils/rest';
const successRes = Promise.resolve({
json: () =>
Promise.resolve({
data: 'hello world',
status: 'ok',
message: 'hello world',
}),
});

test('Successfully POSTs to the given endpoint & stringifies body', async () => {
const fetchStub = jest.fn().mockImplementation(() => successRes);
(fetch as any) = fetchStub;

await expect(post('https://example.com/hello/world', API_KEY, { public_address: '0x0123' })).resolves.toBe(
'hello world',
);
await expect(post('https://example.com/hello/world', API_KEY, { public_address: '0x0123' })).resolves.toEqual({
message: 'hello world',
});

const fetchArguments = fetchStub.mock.calls[0];
expect(fetchArguments).toEqual([
Expand Down