Skip to content

Commit 31a90c9

Browse files
authored
feat: Add batch emails API support (#43)
1 parent 59a9684 commit 31a90c9

File tree

8 files changed

+87
-3
lines changed

8 files changed

+87
-3
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
},
1717
"require-dev": {
1818
"friendsofphp/php-cs-fixer": "^3.13",
19-
"pestphp/pest": "^2.0",
20-
"pestphp/pest-plugin-mock": "^2.0"
19+
"mockery/mockery": "^1.6",
20+
"pestphp/pest": "^2.0"
2121
},
2222
"autoload": {
2323
"psr-4": {

phpunit.xml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
44
colors="true"
55
cacheDirectory=".phpunit.cache"
6+
beStrictAboutOutputDuringTests="true"
67
>
78
<testsuites>
89
<testsuite name="default">

src/Client.php

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Client used to send requests to the Resend API.
1111
*
1212
* @property \Resend\Service\ApiKey $apiKeys
13+
* @property \Resend\Service\Batch $batch
1314
* @property \Resend\Service\Domain $domains
1415
* @property \Resend\Service\Email $emails
1516
*/

src/Service/Batch.php

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

src/Service/ServiceFactory.php

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class ServiceFactory
1313
*/
1414
private static array $classMap = [
1515
'apiKeys' => ApiKey::class,
16+
'batch' => Batch::class,
1617
'domains' => Domain::class,
1718
'emails' => Email::class,
1819
];

tests/Fixtures/Email.php

+14
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,17 @@ function email(): array
99
'created_at' => '2022-07-25T00:28:32.493138+00:00',
1010
];
1111
}
12+
13+
function batch(): array
14+
{
15+
return [
16+
'data' => [
17+
[
18+
'id' => '49a3999c-0ce1-4ea6-ab68-afcd6dc2e794',
19+
],
20+
[
21+
'id' => '49a3999c-0ce1-4ea6-ab68-afcd6dc2e794',
22+
],
23+
],
24+
];
25+
}

tests/Pest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ function mockClient(string $method, string $resource, array $parameters, array|s
1515
$transporter
1616
->shouldReceive($methodName)
1717
->once()
18-
->withArgs(function (Payload $payload) use ($method, $resource) {
18+
->withArgs(function (Payload $payload) use ($method, $resource, $parameters) {
1919
$baseUri = BaseUri::from('api.resend.com');
2020
$headers = Headers::withAuthorization(ApiKey::from('foo'));
2121

2222
$request = $payload->toRequest($baseUri, $headers);
2323

24+
if ($method === 'POST' && (string) $request->getBody() !== json_encode($parameters)) {
25+
return false;
26+
}
27+
2428
return $request->getMethod() === $method
2529
&& $request->getHeader('User-Agent')[0] === 'resend-php/' . Resend::VERSION
2630
&& $request->getUri()->getPath() === "/{$resource}";

tests/Service/Batch.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Resend\Collection;
4+
5+
it('can send a batch of emails', function () {
6+
$payload = [
7+
[
8+
'to' => '[email protected]',
9+
'from' => '[email protected]',
10+
'subject' => 'Acme',
11+
'text' => 'it works!',
12+
],
13+
[
14+
'to' => '[email protected]',
15+
'from' => '[email protected]',
16+
'subject' => 'Acme',
17+
'text' => 'it works!',
18+
],
19+
];
20+
21+
$client = mockClient('POST', 'emails/batch', $payload, batch());
22+
23+
$result = $client->batch->send($payload);
24+
25+
expect($result)->toBeInstanceOf(Collection::class)
26+
->data->toBeArray();
27+
});

0 commit comments

Comments
 (0)