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
186 changes: 121 additions & 65 deletions src/api-keys/api-keys.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ describe('API Keys', () => {
await expect(
resend.apiKeys.create(payload),
).resolves.toMatchInlineSnapshot(`
{
"data": {
"id": "430eed87-632a-4ea6-90db-0aace67ec228",
"token": "re_PKr4RCko_Lhm9ost2YjNCctnPjbLw8Nqk",
},
"error": null,
}
`);
{
"data": {
"id": "430eed87-632a-4ea6-90db-0aace67ec228",
"token": "re_PKr4RCko_Lhm9ost2YjNCctnPjbLw8Nqk",
},
"error": null,
"headers": {
"authorization": "Bearer re_924b3rjh2387fbewf823",
"content-type": "application/json",
},
}
`);
});

it('throws error when missing name', async () => {
Expand All @@ -71,14 +75,18 @@ describe('API Keys', () => {
const result = resend.apiKeys.create(payload);

await expect(result).resolves.toMatchInlineSnapshot(`
{
"data": null,
"error": {
"message": "String must contain at least 1 character(s)",
"name": "validation_error",
},
}
`);
{
"data": null,
"error": {
"message": "String must contain at least 1 character(s)",
"name": "validation_error",
},
"headers": {
"authorization": "Bearer re_924b3rjh2387fbewf823",
"content-type": "application/json",
},
}
`);
});

describe('with access', () => {
Expand Down Expand Up @@ -106,14 +114,18 @@ describe('API Keys', () => {
await expect(
resend.apiKeys.create(payload),
).resolves.toMatchInlineSnapshot(`
{
"data": {
"id": "430eed87-632a-4ea6-90db-0aace67ec228",
"token": "re_PKr4RCko_Lhm9ost2YjNCctnPjbLw8Nqk",
},
"error": null,
}
`);
{
"data": {
"id": "430eed87-632a-4ea6-90db-0aace67ec228",
"token": "re_PKr4RCko_Lhm9ost2YjNCctnPjbLw8Nqk",
},
"error": null,
"headers": {
"authorization": "Bearer re_924b3rjh2387fbewf823",
"content-type": "application/json",
},
}
`);
});

it('creates api key with access `sending_access`', async () => {
Expand All @@ -139,14 +151,18 @@ describe('API Keys', () => {
await expect(
resend.apiKeys.create(payload),
).resolves.toMatchInlineSnapshot(`
{
"data": {
"id": "430eed87-632a-4ea6-90db-0aace67ec228",
"token": "re_PKr4RCko_Lhm9ost2YjNCctnPjbLw8Nqk",
},
"error": null,
}
`);
{
"data": {
"id": "430eed87-632a-4ea6-90db-0aace67ec228",
"token": "re_PKr4RCko_Lhm9ost2YjNCctnPjbLw8Nqk",
},
"error": null,
"headers": {
"authorization": "Bearer re_924b3rjh2387fbewf823",
"content-type": "application/json",
},
}
`);
});

it('throws error with wrong access', async () => {
Expand All @@ -173,14 +189,18 @@ describe('API Keys', () => {
await expect(
resend.apiKeys.create(payload),
).resolves.toMatchInlineSnapshot(`
{
"data": null,
"error": {
"message": "Access must be "full_access" | "sending_access"",
"name": "invalid_access",
},
}
`);
{
"data": null,
"error": {
"message": "Access must be "full_access" | "sending_access"",
"name": "invalid_access",
},
"headers": {
"authorization": "Bearer re_924b3rjh2387fbewf823",
"content-type": "application/json",
},
}
`);
});
});

Expand Down Expand Up @@ -281,7 +301,9 @@ describe('API Keys', () => {
describe('when no pagination options are provided', () => {
it('lists api keys', async () => {
mockSuccessResponse(response, {
headers: { Authorization: 'Bearer re_924b3rjh2387fbewf823' },
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');
Expand All @@ -290,6 +312,10 @@ describe('API Keys', () => {
expect(result).toEqual({
data: response,
error: null,
headers: {
authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
'content-type': 'application/json',
},
});

expect(fetchMock).toHaveBeenCalledWith(
Expand All @@ -305,14 +331,20 @@ describe('API Keys', () => {
describe('when pagination options are provided', () => {
it('passes limit param and returns a response', async () => {
mockSuccessResponse(response, {
headers: { Authorization: 'Bearer re_924b3rjh2387fbewf823' },
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');
const result = await resend.apiKeys.list({ limit: 1 });
expect(result).toEqual({
data: response,
error: null,
headers: {
authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
'content-type': 'application/json',
},
});

expect(fetchMock).toHaveBeenCalledWith(
Expand All @@ -326,7 +358,9 @@ describe('API Keys', () => {

it('passes after param and returns a response', async () => {
mockSuccessResponse(response, {
headers: { Authorization: 'Bearer re_924b3rjh2387fbewf823' },
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');
Expand All @@ -337,6 +371,10 @@ describe('API Keys', () => {
expect(result).toEqual({
data: response,
error: null,
headers: {
authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
'content-type': 'application/json',
},
});

expect(fetchMock).toHaveBeenCalledWith(
Expand All @@ -350,7 +388,9 @@ describe('API Keys', () => {

it('passes before param and returns a response', async () => {
mockSuccessResponse(response, {
headers: { Authorization: 'Bearer re_924b3rjh2387fbewf823' },
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');
Expand All @@ -361,6 +401,10 @@ describe('API Keys', () => {
expect(result).toEqual({
data: response,
error: null,
headers: {
authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
'content-type': 'application/json',
},
});

expect(fetchMock).toHaveBeenCalledWith(
Expand Down Expand Up @@ -390,11 +434,15 @@ describe('API Keys', () => {
const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');

await expect(resend.apiKeys.remove(id)).resolves.toMatchInlineSnapshot(`
{
"data": {},
"error": null,
}
`);
{
"data": {},
"error": null,
"headers": {
"authorization": "Bearer re_924b3rjh2387fbewf823",
"content-type": "application/json",
},
}
`);
});

it('throws error when missing id', async () => {
Expand All @@ -416,14 +464,18 @@ describe('API Keys', () => {
const result = resend.apiKeys.remove('');

await expect(result).resolves.toMatchInlineSnapshot(`
{
"data": null,
"error": {
"message": "Something went wrong",
"name": "application_error",
},
}
`);
{
"data": null,
"error": {
"message": "Something went wrong",
"name": "application_error",
},
"headers": {
"authorization": "Bearer re_924b3rjh2387fbewf823",
"content-type": "application/json",
},
}
`);
});

it('throws error when wrong id', async () => {
Expand All @@ -447,14 +499,18 @@ describe('API Keys', () => {
);

await expect(result).resolves.toMatchInlineSnapshot(`
{
"data": null,
"error": {
"message": "API key not found",
"name": "not_found",
},
}
`);
{
"data": null,
"error": {
"message": "API key not found",
"name": "not_found",
},
"headers": {
"authorization": "Bearer re_924b3rjh2387fbewf823",
"content-type": "application/json",
},
}
`);
});
});
});
12 changes: 2 additions & 10 deletions src/api-keys/interfaces/create-api-key-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PostOptions } from '../../common/interfaces';
import type { ErrorResponse } from '../../interfaces';
import type { Response } from '../../interfaces';

export interface CreateApiKeyOptions {
name: string;
Expand All @@ -14,12 +14,4 @@ export interface CreateApiKeyResponseSuccess {
id: string;
}

export type CreateApiKeyResponse =
| {
data: CreateApiKeyResponseSuccess;
error: null;
}
| {
data: null;
error: ErrorResponse;
};
export type CreateApiKeyResponse = Response<CreateApiKeyResponseSuccess>;
12 changes: 2 additions & 10 deletions src/api-keys/interfaces/list-api-keys.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PaginationOptions } from '../../common/interfaces';
import type { ErrorResponse } from '../../interfaces';
import type { Response } from '../../interfaces';
import type { ApiKey } from './api-key';

export type ListApiKeysOptions = PaginationOptions;
Expand All @@ -10,12 +10,4 @@ export type ListApiKeysResponseSuccess = {
data: Pick<ApiKey, 'name' | 'id' | 'created_at'>[];
};

export type ListApiKeysResponse =
| {
data: ListApiKeysResponseSuccess;
error: null;
}
| {
data: null;
error: ErrorResponse;
};
export type ListApiKeysResponse = Response<ListApiKeysResponseSuccess>;
12 changes: 2 additions & 10 deletions src/api-keys/interfaces/remove-api-keys.interface.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import type { ErrorResponse } from '../../interfaces';
import type { Response } from '../../interfaces';

// biome-ignore lint/complexity/noBannedTypes: allow empty types
export type RemoveApiKeyResponseSuccess = {};

export type RemoveApiKeyResponse =
| {
data: RemoveApiKeyResponseSuccess;
error: null;
}
| {
data: null;
error: ErrorResponse;
};
export type RemoveApiKeyResponse = Response<RemoveApiKeyResponseSuccess>;
Loading
Loading