Skip to content

Commit e1c4877

Browse files
authored
Merge pull request #107 from bengriffin1/bengriffin1-post-header-bugfix
[FIX]: adding json header
2 parents 1778401 + ccb51af commit e1c4877

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

src/modules/nft/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export class NFTModule extends BaseModule {
1717
quantity,
1818
destination_address: destinationAddress,
1919
};
20-
const response = await post(`${this.sdk.apiBaseUrl}${v1StartMint721Path}`, this.sdk.secretApiKey, body);
20+
const response = await post(`${this.sdk.apiBaseUrl}${v1StartMint721Path}`, this.sdk.secretApiKey, body, {
21+
'Content-Type': 'application/json',
22+
});
2123
if (!isMintRequest(response) || response.status !== successStatus) throw mintingError();
2224
const request: MintRequest = response;
2325
return request;
@@ -36,7 +38,9 @@ export class NFTModule extends BaseModule {
3638
destination_address: destinationAddress,
3739
token_id: tokenId,
3840
};
39-
const response = await post(`${this.sdk.apiBaseUrl}${v1StartMint1155Path}`, this.sdk.secretApiKey, body);
41+
const response = await post(`${this.sdk.apiBaseUrl}${v1StartMint1155Path}`, this.sdk.secretApiKey, body, {
42+
'Content-Type': 'application/json',
43+
});
4044
if (!isMintRequest(response) || response.status !== successStatus) throw mintingError();
4145
const request: MintRequest = response;
4246
return request;

src/utils/rest.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ interface MagicAPIResponse<TData = {}> {
99
status?: string | number;
1010
}
1111

12+
interface Headers {
13+
[key: string]: any;
14+
}
15+
1216
/**
1317
* Performs a `fetch` to the given URL with the configured `init` object.
1418
*/
@@ -45,10 +49,15 @@ export function post<TBody extends Record<string, string | number | boolean> = {
4549
url: string,
4650
secretApiKey: string,
4751
body: TBody,
52+
additionalHeaders?: Headers,
4853
) {
54+
let headers: Headers = { 'X-Magic-Secret-key': secretApiKey };
55+
if (additionalHeaders) {
56+
headers = { ...headers, ...additionalHeaders };
57+
}
4958
return emitRequest<TResponse>(url, {
5059
method: 'POST',
51-
headers: { 'X-Magic-Secret-key': secretApiKey },
60+
headers,
5261
body: JSON.stringify(body),
5362
});
5463
}

test/spec/modules/nft/mint1155.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ test('Successfully POSTs to 1155 minting endpoint', async () => {
4545
'https://example.com/v1/admin/nft/mint/1155_mint',
4646
API_KEY,
4747
{ contract_id: '0xfoo', quantity: 1, destination_address: '0xbar', token_id: 0 },
48+
{ 'Content-Type': 'application/json' },
4849
]);
4950
});
5051

test/spec/modules/nft/mint721.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ test('Successfully POSTs to 721 minting endpoint', async () => {
4545
'https://example.com/v1/admin/nft/mint/721_mint',
4646
API_KEY,
4747
{ contract_id: '0xfoo', quantity: 1, destination_address: '0xbar' },
48+
{ 'Content-Type': 'application/json' },
4849
]);
4950
});
5051

test/spec/utils/rest/post.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,30 @@ test('Successfully POSTs to the given endpoint & stringifies body', async () =>
2828
},
2929
]);
3030
});
31+
32+
test('Successfully POSTs to the given endpoint and adds JSON header', async () => {
33+
const fetchStub = jest.fn().mockImplementation(() => successRes);
34+
(fetch as any) = fetchStub;
35+
36+
await expect(
37+
post(
38+
'https://example.com/hello/world',
39+
API_KEY,
40+
{ public_address: '0x0123' },
41+
{ 'Content-Type': 'application/json' },
42+
),
43+
).resolves.toBe('hello world');
44+
45+
const fetchArguments = fetchStub.mock.calls[0];
46+
expect(fetchArguments).toEqual([
47+
'https://example.com/hello/world',
48+
{
49+
method: 'POST',
50+
headers: {
51+
'X-Magic-Secret-key': API_KEY,
52+
'Content-Type': 'application/json',
53+
},
54+
body: '{"public_address":"0x0123"}',
55+
},
56+
]);
57+
});

0 commit comments

Comments
 (0)