Skip to content

Commit 18463b6

Browse files
authored
Merge pull request #20 from resendlabs/feat-emails-api
2 parents 5de24fc + 8172159 commit 18463b6

17 files changed

+194
-15
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Then, interact with Resend's API:
2222
```php
2323
$resend = Resend::client('re_123456789');
2424

25-
$resend->sendEmail([
25+
$resend->emails->create([
2626
'from' => '[email protected]',
2727
'to' => '[email protected]',
2828
'subject' => 'hello world',

examples/email.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// Attempt to send out an email...
1010
try {
1111
// Send an email using plain text...
12-
$result = $resend->sendEmail([
12+
$result = $resend->emails->send([
1313
'from' => $_ENV['MAIL_FROM_ADDRESS'],
1414
'to' => '[email protected]',
1515
'subject' => 'hello world',

src/Client.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
use Resend\Contracts\Transporter;
66
use Resend\Service\ServiceFactory;
7-
use Resend\ValueObjects\Transporter\Payload;
87

98
/**
109
* Client used to send requests to the Resend API.
1110
*
1211
* @property \Resend\Service\ApiKey $apiKeys
1312
* @property \Resend\Service\Domain $domains
13+
* @property \Resend\Service\Email $emails
1414
*/
1515
class Client
1616
{
@@ -31,15 +31,12 @@ public function __construct(
3131
/**
3232
* Send an email with the given parameters.
3333
*
34-
* @see https://resend.com/docs/api-reference/send-email#body-parameters
34+
* @deprecated
35+
* @see @see https://resend.com/docs/api-reference/emails/send-email#body-parameters
3536
*/
3637
public function sendEmail(array $parameters): Email
3738
{
38-
$payload = Payload::create('email', $parameters);
39-
40-
$result = $this->transporter->request($payload);
41-
42-
return Email::from($result);
39+
return $this->emails->send($parameters);
4340
}
4441

4542
/**

src/Resource.php

+12
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ public function __debugInfo(): array
107107
return $this->getAttributes();
108108
}
109109

110+
/**
111+
* {@inheritdoc}
112+
*/
110113
public function offsetExists(mixed $offset): bool
111114
{
112115
try {
@@ -116,16 +119,25 @@ public function offsetExists(mixed $offset): bool
116119
}
117120
}
118121

122+
/**
123+
* {@inheritdoc}
124+
*/
119125
public function offsetGet(mixed $offset): mixed
120126
{
121127
return $this->getAttribute($offset);
122128
}
123129

130+
/**
131+
* {@inheritdoc}
132+
*/
124133
public function offsetSet(mixed $offset, mixed $value): void
125134
{
126135
throw new BadMethodCallException('Cannot set resource attributes.');
127136
}
128137

138+
/**
139+
* {@inheritdoc}
140+
*/
129141
public function offsetUnset(mixed $offset): void
130142
{
131143
throw new BadMethodCallException('Cannot unset resource attributes.');

src/Service/ApiKey.php

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ final class ApiKey extends Service
88
{
99
/**
1010
* Create a new API key.
11+
*
12+
* @see https://resend.com/docs/api-reference/api-keys/create-api-key#body-parameters
1113
*/
1214
public function create(array $parameters): \Resend\ApiKey
1315
{
@@ -22,6 +24,8 @@ public function create(array $parameters): \Resend\ApiKey
2224
* List all API keys.
2325
*
2426
* @return \Resend\Collection<\Resend\ApiKey>
27+
*
28+
* @see https://resend.com/docs/api-reference/api-keys/list-api-keys
2529
*/
2630
public function list(): \Resend\Collection
2731
{
@@ -34,6 +38,8 @@ public function list(): \Resend\Collection
3438

3539
/**
3640
* Remove an API key with the given ID.
41+
*
42+
* @see https://resend.com/docs/api-reference/api-keys/delete-api-key#path-parameters
3743
*/
3844
public function remove(string $id): \Resend\ApiKey
3945
{

src/Service/Domain.php

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ final class Domain extends Service
88
{
99
/**
1010
* Add a new domain.
11+
*
12+
* @see https://resend.com/docs/api-reference/domains/create-domain#body-parameters
1113
*/
1214
public function create(array $parameters): \Resend\Domain
1315
{
@@ -22,6 +24,8 @@ public function create(array $parameters): \Resend\Domain
2224
* List all domains.
2325
*
2426
* @return \Resend\Collection<\Resend\Domain>
27+
*
28+
* @see https://resend.com/docs/api-reference/domains/list-domains
2529
*/
2630
public function list(): \Resend\Collection
2731
{
@@ -34,6 +38,8 @@ public function list(): \Resend\Collection
3438

3539
/**
3640
* Remove a domain with the given ID.
41+
*
42+
* @see https://resend.com/docs/api-reference/domains/delete-domain#path-parameters
3743
*/
3844
public function remove(string $id): \Resend\Domain
3945
{
@@ -46,6 +52,8 @@ public function remove(string $id): \Resend\Domain
4652

4753
/**
4854
* Verify a domain with the given ID.
55+
*
56+
* @see https://resend.com/docs/api-reference/domains/verify-domain#path-parameters
4957
*/
5058
public function verify(string $id): \Resend\Domain
5159
{

src/Service/Email.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Resend\Service;
4+
5+
use Resend\ValueObjects\Transporter\Payload;
6+
7+
final class Email extends Service
8+
{
9+
/**
10+
* Retrieve an email with the given ID.
11+
*
12+
* @see https://resend.com/docs/api-reference/emails/retrieve-email#path-parameters
13+
*/
14+
public function get(string $id): \Resend\Email
15+
{
16+
$payload = Payload::get('emails', $id);
17+
18+
$result = $this->transporter->request($payload);
19+
20+
return $this->createResource('emails', $result);
21+
}
22+
23+
/**
24+
* Send an email with the given parameters.
25+
*
26+
* @see https://resend.com/docs/api-reference/emails/send-email#body-parameters
27+
*/
28+
public function create(array $parameters): \Resend\Email
29+
{
30+
$payload = Payload::create('emails', $parameters);
31+
32+
$result = $this->transporter->request($payload);
33+
34+
return $this->createResource('emails', $result);
35+
}
36+
37+
/**
38+
* Send an email with the given parameters.
39+
*
40+
* @see https://resend.com/docs/api-reference/emails/send-email#body-parameters
41+
*/
42+
public function send(array $parameters): \Resend\Email
43+
{
44+
return $this->create($parameters);
45+
}
46+
}

src/Service/Service.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Resend\Collection;
77
use Resend\Contracts\Transporter;
88
use Resend\Domain;
9+
use Resend\Email;
910
use Resend\Resource;
1011

1112
abstract class Service
@@ -16,6 +17,7 @@ abstract class Service
1617
protected $mapping = [
1718
'api-keys' => ApiKey::class,
1819
'domains' => Domain::class,
20+
'emails' => Email::class,
1921
];
2022

2123
/**

src/Service/ServiceFactory.php

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ final class ServiceFactory
1414
private static array $classMap = [
1515
'apiKeys' => ApiKey::class,
1616
'domains' => Domain::class,
17+
'emails' => Email::class,
1718
];
1819

1920
/**

src/ValueObjects/ResourceUri.php

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public static function list(string $resource): self
3131
return new self($resource);
3232
}
3333

34+
/**
35+
* Create a new Resource URI value object that retrieves the given resource.
36+
*/
37+
public static function get(string $resource, string $id): self
38+
{
39+
return new self("{$resource}/{$id}");
40+
}
41+
3442
/**
3543
* Create a new Resource URI value object that deletes the given resource.
3644
*/

src/ValueObjects/Transporter/Payload.php

+9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ public static function list(string $resource): self
3131
return new self($contentType, $method, $uri);
3232
}
3333

34+
public static function get(string $resource, string $id): self
35+
{
36+
$contentType = ContentType::JSON;
37+
$method = Method::GET;
38+
$uri = ResourceUri::get($resource, $id);
39+
40+
return new self($contentType, $method, $uri);
41+
}
42+
3443
/**
3544
* Create a new Transporter Payload instance.
3645
*/

tests/Client.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
use Resend\Service\ApiKey;
55

66
test('send email', function () {
7-
$client = mockClient('POST', 'email', [
7+
$client = mockClient('POST', 'emails', [
88
'to' => '[email protected]',
99
], email());
1010

11+
// Use deprecated method until it is removed...
1112
$result = $client->sendEmail([
1213
'to' => '[email protected]',
1314
]);

tests/Fixtures/ApiKey.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function apiKey(): array
55
return [
66
'id' => '71af5cc3-b449-4ac4-888a-5ab9f55e1dbb',
77
'name' => 'Production',
8-
'created_at' => '',
8+
'created_at' => '2022-07-25T00:28:32.493138+00:00',
99
];
1010
}
1111

@@ -16,12 +16,12 @@ function apiKeys(): array
1616
[
1717
'id' => '71af5cc3-b449-4ac4-888a-5ab9f55e1dbb',
1818
'name' => 'Production',
19-
'created_at' => '',
19+
'created_at' => '2022-07-25T00:28:32.493138+00:00',
2020
],
2121
[
2222
'id' => '823ad493-7081-4344-b476-fb0db4bd1e62',
2323
'name' => 'Development',
24-
'created_at' => '',
24+
'created_at' => '2022-07-25T00:28:32.493138+00:00',
2525
],
2626
],
2727
];

tests/Fixtures/Domain.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
function domain(): array
4+
{
5+
return [
6+
'id' => '4dd369bc-aa82-4ff3-97de-514ae3000ee0',
7+
'name' => 'resend.dev',
8+
'status' => 'pending',
9+
'region' => 'us-east-1',
10+
'created_at' => '2022-07-25T00:28:32.493138+00:00',
11+
];
12+
}
13+
14+
function domains(): array
15+
{
16+
return [
17+
'data' => [
18+
[
19+
'id' => '71af5cc3-b449-4ac4-888a-5ab9f55e1dbb',
20+
'name' => 'resend.dev',
21+
'status' => 'pending',
22+
'region' => 'us-east-1',
23+
'created_at' => '2022-07-25T00:28:32.493138+00:00',
24+
],
25+
[
26+
'id' => '823ad493-7081-4344-b476-fb0db4bd1e62',
27+
'name' => 'resend.com',
28+
'status' => 'pending',
29+
'region' => 'us-east-1',
30+
'created_at' => '2022-07-25T00:28:32.493138+00:00',
31+
],
32+
],
33+
];
34+
}

tests/Service/ApiKey.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
use Resend\ApiKey;
44

55
it('can delete an API key resource', function () {
6-
$client = mockClient('DELETE', 'api-keys/re_123456', [], apiKey());
6+
$client = mockClient('DELETE', 'api-keys/71af5cc3-b449-4ac4-888a-5ab9f55e1dbb', [], apiKey());
77

8-
$result = $client->apiKeys->remove('re_123456');
8+
$result = $client->apiKeys->remove('71af5cc3-b449-4ac4-888a-5ab9f55e1dbb');
99

1010
expect($result)->toBeInstanceOf(ApiKey::class);
1111
});

tests/Service/Domain.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
use Resend\Collection;
4+
use Resend\Domain;
5+
6+
it('can create a domain resource', function () {
7+
$client = mockClient('POST', 'domains', [
8+
'name' => 'resend.dev',
9+
], domain());
10+
11+
$result = $client->domains->create([
12+
'name' => 'resend.dev',
13+
]);
14+
15+
expect($result)->toBeInstanceOf(Domain::class)
16+
->id->toBe('4dd369bc-aa82-4ff3-97de-514ae3000ee0');
17+
});
18+
19+
it('can get a list of domain resources', function () {
20+
$client = mockClient('GET', 'domains', [], domains());
21+
22+
$result = $client->domains->list();
23+
24+
expect($result)->toBeInstanceOf(Collection::class)
25+
->data->toBeArray();
26+
});
27+
28+
it('can remove a domain resource', function () {
29+
$client = mockClient('DELETE', 'domains/4dd369bc-aa82-4ff3-97de-514ae3000ee0', [], domain());
30+
31+
$result = $client->domains->remove('4dd369bc-aa82-4ff3-97de-514ae3000ee0');
32+
33+
expect($result)->toBeInstanceOf(Domain::class);
34+
});
35+
36+
it('can verify a domain resource', function () {
37+
$client = mockClient('POST', 'domains/re_1234567/verify', [], domain());
38+
39+
$result = $client->domains->verify('re_1234567');
40+
41+
expect($result)->toBeInstanceOf(Domain::class)
42+
->id->toBe('4dd369bc-aa82-4ff3-97de-514ae3000ee0');
43+
});

tests/Service/Email.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use Resend\Email;
4+
5+
it('can get an email resource', function () {
6+
$client = mockClient('GET', 'emails/49a3999c-0ce1-4ea6-ab68-afcd6dc2e794', [], email());
7+
8+
$result = $client->emails->get('49a3999c-0ce1-4ea6-ab68-afcd6dc2e794');
9+
10+
expect($result)->toBeInstanceOf(Email::class)
11+
->id->toBe('49a3999c-0ce1-4ea6-ab68-afcd6dc2e794');
12+
});

0 commit comments

Comments
 (0)