Skip to content

[FIX]: adding json header #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
8 changes: 6 additions & 2 deletions src/modules/nft/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export class NFTModule extends BaseModule {
quantity,
destination_address: destinationAddress,
};
const response = await post(`${this.sdk.apiBaseUrl}${v1StartMint721Path}`, this.sdk.secretApiKey, body);
const response = await post(`${this.sdk.apiBaseUrl}${v1StartMint721Path}`, this.sdk.secretApiKey, body, {
'Content-Type': 'application/json',
});
if (!isMintRequest(response) || response.status !== successStatus) throw mintingError();
const request: MintRequest = response;
return request;
Expand All @@ -36,7 +38,9 @@ export class NFTModule extends BaseModule {
destination_address: destinationAddress,
token_id: tokenId,
};
const response = await post(`${this.sdk.apiBaseUrl}${v1StartMint1155Path}`, this.sdk.secretApiKey, body);
const response = await post(`${this.sdk.apiBaseUrl}${v1StartMint1155Path}`, this.sdk.secretApiKey, body, {
'Content-Type': 'application/json',
});
if (!isMintRequest(response) || response.status !== successStatus) throw mintingError();
const request: MintRequest = response;
return request;
Expand Down
11 changes: 10 additions & 1 deletion src/utils/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ interface MagicAPIResponse<TData = {}> {
status?: string | number;
}

interface Headers {
[key: string]: any;
}

/**
* Performs a `fetch` to the given URL with the configured `init` object.
*/
Expand Down Expand Up @@ -45,10 +49,15 @@ export function post<TBody extends Record<string, string | number | boolean> = {
url: string,
secretApiKey: string,
body: TBody,
additionalHeaders?: Headers,
) {
let headers: Headers = { 'X-Magic-Secret-key': secretApiKey };
if (additionalHeaders) {
headers = { ...headers, ...additionalHeaders };
}
return emitRequest<TResponse>(url, {
method: 'POST',
headers: { 'X-Magic-Secret-key': secretApiKey },
headers,
body: JSON.stringify(body),
});
}
Expand Down
1 change: 1 addition & 0 deletions test/spec/modules/nft/mint1155.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ test('Successfully POSTs to 1155 minting endpoint', async () => {
'https://example.com/v1/admin/nft/mint/1155_mint',
API_KEY,
{ contract_id: '0xfoo', quantity: 1, destination_address: '0xbar', token_id: 0 },
{ 'Content-Type': 'application/json' },
]);
});

Expand Down
1 change: 1 addition & 0 deletions test/spec/modules/nft/mint721.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ test('Successfully POSTs to 721 minting endpoint', async () => {
'https://example.com/v1/admin/nft/mint/721_mint',
API_KEY,
{ contract_id: '0xfoo', quantity: 1, destination_address: '0xbar' },
{ 'Content-Type': 'application/json' },
]);
});

Expand Down
27 changes: 27 additions & 0 deletions test/spec/utils/rest/post.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,30 @@ test('Successfully POSTs to the given endpoint & stringifies body', async () =>
},
]);
});

test('Successfully POSTs to the given endpoint and adds JSON header', async () => {
const fetchStub = jest.fn().mockImplementation(() => successRes);
(fetch as any) = fetchStub;

await expect(
post(
'https://example.com/hello/world',
API_KEY,
{ public_address: '0x0123' },
{ 'Content-Type': 'application/json' },
),
).resolves.toBe('hello world');

const fetchArguments = fetchStub.mock.calls[0];
expect(fetchArguments).toEqual([
'https://example.com/hello/world',
{
method: 'POST',
headers: {
'X-Magic-Secret-key': API_KEY,
'Content-Type': 'application/json',
},
body: '{"public_address":"0x0123"}',
},
]);
});