Skip to content

Commit 3945788

Browse files
Use toaster Endpoints (#131)
* use new endpoints * fix: emitRequest test * ubuntu-latest
1 parent 7bb64ad commit 3945788

File tree

11 files changed

+37
-58
lines changed

11 files changed

+37
-58
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66

77
jobs:
88
publish:
9-
runs-on: ubuntu-20.04
9+
runs-on: ubuntu-latest
1010
name: Publish
1111
if: "!contains(github.event.head_commit.message, 'ci skip') && !contains(github.event.head_commit.message, 'skip ci')"
1212
steps:

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010

1111
jobs:
1212
test:
13-
runs-on: ubuntu-20.04
13+
runs-on: ubuntu-latest
1414
name: Run style/security checks & tests
1515
container:
1616
image: node:16-slim

src/core/sdk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class MagicAdminSDK {
3939
public readonly secretApiKey?: string,
4040
options?: MagicAdminSDKAdditionalConfiguration,
4141
) {
42-
const endpoint = options?.endpoint ?? 'https://api.magic.link';
42+
const endpoint = options?.endpoint ?? 'https://api.toaster.magic.link';
4343
this.apiBaseUrl = endpoint.replace(/\/+$/, '');
4444
this.clientId = options?.clientId ?? null;
4545
// Assign API Modules
@@ -53,7 +53,7 @@ export class MagicAdminSDK {
5353

5454
let hydratedOptions = options ?? {};
5555

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

5959
if (!hydratedOptions.clientId) {

src/modules/users/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class UsersModule extends BaseModule {
1010
public async logoutByIssuer(issuer: string): Promise<void> {
1111
if (!this.sdk.secretApiKey) throw createApiKeyMissingError();
1212
const body = { issuer };
13-
await post(`${this.sdk.apiBaseUrl}/v2/admin/auth/user/logout`, this.sdk.secretApiKey, body);
13+
await post(`${this.sdk.apiBaseUrl}/v1/admin/user/logout`, this.sdk.secretApiKey, body);
1414
}
1515

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

6868
return {
6969
issuer: data.issuer ?? null,

src/utils/rest.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,17 @@ import { RequestInit } from 'node-fetch';
33
import { fetch } from './fetch';
44
import { createServiceError } from '../core/sdk-exceptions';
55

6-
interface MagicAPIResponse<TData> {
7-
data?: TData;
8-
error_code?: string;
9-
message?: string;
10-
status?: string | number;
11-
}
12-
136
/**
147
* Performs a `fetch` to the given URL with the configured `init` object.
158
*/
169
async function emitRequest<TResponse>(url: string, init?: RequestInit): Promise<Partial<TResponse>> {
17-
const json: MagicAPIResponse<TResponse> = await fetch(url, init)
10+
const response = await fetch(url, init)
1811
.then((res) => res.json())
1912
.catch((err) => {
2013
throw createServiceError(err);
2114
});
2215

23-
if (json.status !== 'ok') {
24-
throw createServiceError(json);
25-
}
26-
27-
return json.data ?? {};
16+
return response;
2817
}
2918

3019
/**

test/lib/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const API_FULL_URL = 'https://api.magic.link';
1+
export const API_FULL_URL = 'https://api.toaster.magic.link';
22
export const API_KEY = 'sk_test_123';
33

44
export const VALID_DIDT =

test/spec/modules/users/getMetadataByIssuer.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test('Successfully GETs to metadata endpoint via issuer', async () => {
4141

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

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

106106
const getArguments = getStub.mock.calls[0];
107107
expect(getArguments).toEqual([
108-
'https://example.com/v1/admin/auth/user/get',
108+
'https://example.com/v1/admin/user',
109109
API_KEY,
110110
{ issuer: 'did:ethr:0x1234', wallet_type: 'SOLANA'},
111111
]);

test/spec/modules/users/logoutByIssuer.spec.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { createMagicAdminSDK } from '../../../lib/factories';
2-
import { API_KEY } from '../../../lib/constants';
31
import { createApiKeyMissingError } from '../../../../src/core/sdk-exceptions';
42
import { post } from '../../../../src/utils/rest';
3+
import { API_KEY } from '../../../lib/constants';
4+
import { createMagicAdminSDK } from '../../../lib/factories';
55

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

1414
const postArguments = postStub.mock.calls[0];
15-
expect(postArguments).toEqual([
16-
'https://example.com/v2/admin/auth/user/logout',
17-
API_KEY,
18-
{ issuer: 'did:ethr:0x1234' },
19-
]);
15+
expect(postArguments).toEqual(['https://example.com/v1/admin/user/logout', API_KEY, { issuer: 'did:ethr:0x1234' }]);
2016
});
2117

2218
test('Fails POST if API key is missing', async () => {

test/spec/utils/rest/emitRequest.spec.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,18 @@ const failWithTypeError = Promise.resolve({
1717
const failWithBadStatus = Promise.resolve({
1818
json: () =>
1919
Promise.resolve({
20-
status: 'qwerty', // Only 'ok' with succeed
20+
error: 'Something went wrong',
2121
}),
2222
});
2323

2424
const failWithEmptyStatus = Promise.resolve({
2525
json: () =>
26-
Promise.resolve({
27-
// No status defined will assume non-'ok'
28-
}),
26+
Promise.resolve({}),
2927
});
3028

3129
const successResEmptyData = Promise.resolve({
3230
json: () =>
33-
Promise.resolve({
34-
status: 'ok',
35-
}),
31+
Promise.resolve({}),
3632
});
3733

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

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

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

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

54-
const expectedError = createServiceError({ status: 'qwerty' });
55-
56-
await expect(get(URL, API_KEY)).rejects.toThrow(expectedError);
50+
await expect(get(URL, API_KEY)).resolves.toEqual({
51+
error: 'Something went wrong',
52+
});
5753
});
5854

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

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

69-
const expectedError = createServiceError({ status: 'qwerty' });
70-
71-
expect(get(URL, API_KEY)).rejects.toThrow(expectedError);
65+
await expect(get(URL, API_KEY)).resolves.toEqual({});
7266
});

test/spec/utils/rest/get.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ import { get } from '../../../../src/utils/rest';
55
const successRes = Promise.resolve({
66
json: () =>
77
Promise.resolve({
8-
data: 'hello world',
9-
status: 'ok',
8+
message: 'hello world',
109
}),
1110
});
1211

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

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

34-
await expect(get('https://example.com/hello/world', API_KEY)).resolves.toBe('hello world');
33+
await expect(get('https://example.com/hello/world', API_KEY)).resolves.toEqual({
34+
message: 'hello world',
35+
});
3536

3637
const fetchArguments = fetchStub.mock.calls[0];
3738
expect(fetchArguments).toEqual([

0 commit comments

Comments
 (0)