Skip to content

Commit

Permalink
Merge pull request #106 from bengriffin1/bengriffin1-sc73342-response…
Browse files Browse the repository at this point in the history
…-object-updates

[FIX]: updating response from API server to fit new schema
  • Loading branch information
bengriffin1 committed Mar 24, 2023
2 parents e306934 + 1ddb649 commit 5651bce
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 34 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# v1.9.1 (Wed Mar 22 2023)

#### 🐛 Bug Fix

- Update modules/mint/index.ts to account for new API response format [#106](https://github.com/magiclabs/magic-admin-js/pull/106) ([@bengriffin1](https://github.com/bengriffin1))

#### Authors: 1

- Ben Griffin ([@bengriffin1](https://github.com/bengriffin1))

---

# v1.9.0 (Wed Mar 22 2023)

#### 🚀 Enhancement
Expand Down
6 changes: 3 additions & 3 deletions src/core/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import '../utils/shim';
import { TokenModule } from '../modules/token';
import { UsersModule } from '../modules/users';
import { UtilsModule } from '../modules/utils';
import { MintModule } from '../modules/mint';
import { NFTModule } from '../modules/nft';
import { MagicAdminSDKAdditionalConfiguration } from '../types';

export class MagicAdminSDK {
Expand All @@ -28,7 +28,7 @@ export class MagicAdminSDK {
/**
* Contains utilities for Minting and Delivery of NFTs.
*/
public readonly mint: MintModule;
public readonly nft: NFTModule;

constructor(public readonly secretApiKey?: string, options?: MagicAdminSDKAdditionalConfiguration) {
const endpoint = options?.endpoint ?? 'https://api.magic.link';
Expand All @@ -38,6 +38,6 @@ export class MagicAdminSDK {
this.token = new TokenModule(this);
this.users = new UsersModule(this);
this.utils = new UtilsModule(this);
this.mint = new MintModule(this);
this.nft = new NFTModule(this);
}
}
32 changes: 19 additions & 13 deletions src/modules/mint/index.ts → src/modules/nft/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,39 @@ import { post } from '../../utils/rest';
import { createApiKeyMissingError, mintingError } from '../../core/sdk-exceptions';
import { isMintRequest } from '../../utils/type-guards';

const v1StartMint721Path = '/v1/admin/nft/mint/721_mint'
const v1StartMint1155Path = '/v1/admin/nft/mint/1155_mint'
const v1StartMint721Path = '/v1/admin/nft/mint/721_mint';
const v1StartMint1155Path = '/v1/admin/nft/mint/1155_mint';
const successStatus = 'ok';

export class MintModule extends BaseModule {
export class NFTModule extends BaseModule {
public async startMint721(contractId: string, quantity: number, destinationAddress: string): Promise<MintRequest> {
if (!this.sdk.secretApiKey) throw createApiKeyMissingError();
const body = {
contract_id: contractId,
quantity,
destination_address: destinationAddress
contract_id: contractId,
quantity,
destination_address: destinationAddress,
};
const response = await post(`${this.sdk.apiBaseUrl}${v1StartMint721Path}`, this.sdk.secretApiKey, body);
if (!isMintRequest(response)) throw mintingError();
if (!isMintRequest(response) || response.status !== successStatus) throw mintingError();
const request: MintRequest = response;
return request;
}

public async startMint1155(contractId: string, quantity: number, destinationAddress: string, tokenId: number): Promise<MintRequest> {
public async startMint1155(
contractId: string,
quantity: number,
destinationAddress: string,
tokenId: number,
): Promise<MintRequest> {
if (!this.sdk.secretApiKey) throw createApiKeyMissingError();
const body = {
contract_id: contractId,
quantity,
destination_address: destinationAddress,
token_id: tokenId
contract_id: contractId,
quantity,
destination_address: destinationAddress,
token_id: tokenId,
};
const response = await post(`${this.sdk.apiBaseUrl}${v1StartMint1155Path}`, this.sdk.secretApiKey, body);
if (!isMintRequest(response)) throw mintingError();
if (!isMintRequest(response) || response.status !== successStatus) throw mintingError();
const request: MintRequest = response;
return request;
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './didt-types';
export * from './exception-types';
export * from './sdk-types';
export * from './wallet-types';
export * from './mint-types';
export * from './nft-types';
3 changes: 0 additions & 3 deletions src/types/mint-types.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/types/nft-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface MintRequestData {
requestId: string;
}

export interface MintRequest {
data: MintRequestData;
error_code: string;
message: string;
status: string;
}
11 changes: 9 additions & 2 deletions src/utils/type-guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ export function isDIDTClaim(value: any): value is Claim {
);
}

/** Assert `request_id` exists */
/** Assert `value` contains all required MintRequest members */
export function isMintRequest(value: any): value is MintRequest {
return !isNil(value.request_id)
return (
!isNil(value) &&
!isNil(value.data) &&
!isNil(value.error_code) &&
!isNil(value.message) &&
!isNil(value.status) &&
!isNil(value.data.request_id)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@ import { createApiKeyMissingError, mintingError } from '../../../../src/core/sdk
import { post } from '../../../../src/utils/rest';

const successReq = Promise.resolve({
request_id: 'foo_123',
data: {
request_id: 'foo_123',
},
status: 'ok',
error_code: '',
message: '',
});

const malformedReq = Promise.resolve({
abc: 'bar_456',
});

const failReq = Promise.resolve({
error: 'bar_456',
data: {
request_id: '',
},
status: 'error',
error_code: '',
message: '',
});

test('Successfully POSTs to 1155 minting endpoint', async () => {
Expand All @@ -17,7 +31,14 @@ test('Successfully POSTs to 1155 minting endpoint', async () => {
const postStub = jest.fn().mockImplementation(() => successReq);
(post as any) = postStub;

await expect(sdk.mint.startMint1155('0xfoo', 1, '0xbar', 0)).resolves.toEqual({ request_id: 'foo_123' });
await expect(sdk.nft.startMint1155('0xfoo', 1, '0xbar', 0)).resolves.toEqual({
data: {
request_id: 'foo_123',
},
status: 'ok',
error_code: '',
message: '',
});

const postArguments = postStub.mock.calls[0];
expect(postArguments).toEqual([
Expand All @@ -27,15 +48,26 @@ test('Successfully POSTs to 1155 minting endpoint', async () => {
]);
});

test('Throws an error if Minting API was unsuccessful', async () => {
test('Throws an error if Minting API returns well formed error', async () => {
const sdk = createMagicAdminSDK('https://example.com');

const postStub = jest.fn().mockImplementation(() => failReq);
(post as any) = postStub;

const expectedError = mintingError();

await expect(sdk.mint.startMint1155('0xfoo', 1, '0xbar', 0)).rejects.toThrow(expectedError);
await expect(sdk.nft.startMint1155('0xfoo', 1, '0xbar', 0)).rejects.toThrow(expectedError);
});

test('Throws an error if Minting API returns unexpected response', async () => {
const sdk = createMagicAdminSDK('https://example.com');

const postStub = jest.fn().mockImplementation(() => malformedReq);
(post as any) = postStub;

const expectedError = mintingError();

await expect(sdk.nft.startMint1155('0xfoo', 1, '0xbar', 0)).rejects.toThrow(expectedError);
});

test('Fails POST if API key is missing', async () => {
Expand All @@ -47,7 +79,7 @@ test('Fails POST if API key is missing', async () => {

const expectedError = createApiKeyMissingError();

await expect(sdk.mint.startMint1155('0xfoo', 1, '0xbar', 0)).rejects.toThrow(expectedError);
await expect(sdk.nft.startMint1155('0xfoo', 1, '0xbar', 0)).rejects.toThrow(expectedError);

expect(postStub).not.toBeCalled();
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@ import { createApiKeyMissingError, mintingError } from '../../../../src/core/sdk
import { post } from '../../../../src/utils/rest';

const successReq = Promise.resolve({
request_id: 'foo_123',
data: {
request_id: 'foo_123',
},
status: 'ok',
error_code: '',
message: '',
});

const malformedReq = Promise.resolve({
abc: 'bar_456',
});

const failReq = Promise.resolve({
error: 'bar_456',
data: {
request_id: '',
},
status: 'error',
error_code: '',
message: '',
});

test('Successfully POSTs to 721 minting endpoint', async () => {
Expand All @@ -17,7 +31,14 @@ test('Successfully POSTs to 721 minting endpoint', async () => {
const postStub = jest.fn().mockImplementation(() => successReq);
(post as any) = postStub;

await expect(sdk.mint.startMint721('0xfoo', 1, '0xbar')).resolves.toEqual({ request_id: 'foo_123' });
await expect(sdk.nft.startMint721('0xfoo', 1, '0xbar')).resolves.toEqual({
data: {
request_id: 'foo_123',
},
status: 'ok',
error_code: '',
message: '',
});

const postArguments = postStub.mock.calls[0];
expect(postArguments).toEqual([
Expand All @@ -27,15 +48,26 @@ test('Successfully POSTs to 721 minting endpoint', async () => {
]);
});

test('Throws an error if Minting API was unsuccessful', async () => {
test('Throws an error if Minting API returns well formed error', async () => {
const sdk = createMagicAdminSDK('https://example.com');

const postStub = jest.fn().mockImplementation(() => failReq);
(post as any) = postStub;

const expectedError = mintingError();

await expect(sdk.mint.startMint721('0xfoo', 1, '0xbar')).rejects.toThrow(expectedError);
await expect(sdk.nft.startMint721('0xfoo', 1, '0xbar')).rejects.toThrow(expectedError);
});

test('Throws an error if Minting API returns unexpected response', async () => {
const sdk = createMagicAdminSDK('https://example.com');

const postStub = jest.fn().mockImplementation(() => malformedReq);
(post as any) = postStub;

const expectedError = mintingError();

await expect(sdk.nft.startMint721('0xfoo', 1, '0xbar')).rejects.toThrow(expectedError);
});

test('Fails POST if API key is missing', async () => {
Expand All @@ -47,7 +79,7 @@ test('Fails POST if API key is missing', async () => {

const expectedError = createApiKeyMissingError();

await expect(sdk.mint.startMint721('0xfoo', 1, '0xbar')).rejects.toThrow(expectedError);
await expect(sdk.nft.startMint721('0xfoo', 1, '0xbar')).rejects.toThrow(expectedError);

expect(postStub).not.toBeCalled();
});

0 comments on commit 5651bce

Please sign in to comment.