Skip to content

Commit 2af883e

Browse files
committed
Better error handling + support Idempotency-Key
1 parent 9bb14e3 commit 2af883e

File tree

7 files changed

+50
-35
lines changed

7 files changed

+50
-35
lines changed

playground.hxml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
-cp tests
22
-main Playground
33
-js bin/playground.js
4+
-dce full
5+
46
-lib stripe
57
-lib hxnodejs

src/stripe/Stripe.hx

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,32 @@ private typedef Impl = Remote<stripe.api.Stripe>;
1313

1414
@:forward
1515
abstract Stripe(Impl) {
16-
public inline function new(apiKey, client) {
17-
this = new Impl(new AuthedClient(apiKey, client), new RemoteEndpoint(new Host('api.stripe.com', 443)));
16+
public inline function new(apiKey, client:Client, ?idempotencyKey) {
17+
client = new AuthedClient(apiKey, client);
18+
if(idempotencyKey != null) client = new IdempotentClient(idempotencyKey, client);
19+
this = new Impl(client, new RemoteEndpoint(new Host('api.stripe.com', 443)));
1820
}
1921
}
2022

21-
class AuthedClient implements ClientObject {
22-
var apiKey:String;
23+
class AuthedClient extends AppendHeaderClient {
24+
public function new(apiKey, client)
25+
super(AUTHORIZATION, 'Bearer $apiKey', client);
26+
}
27+
28+
class IdempotentClient extends AppendHeaderClient {
29+
public function new(key, client)
30+
super('Idempotency-Key', key, client);
31+
}
32+
33+
class AppendHeaderClient implements ClientObject {
34+
var field:HeaderField;
2335
var client:Client;
24-
public function new(apiKey, client) {
25-
this.apiKey = apiKey;
36+
37+
public function new(name, value, client) {
38+
this.field = new HeaderField(name, value);
2639
this.client = client;
2740
}
2841

29-
public function request(req:OutgoingRequest):Promise<IncomingResponse> {
30-
trace(haxe.Json.stringify(apiKey));
31-
return client.request(new OutgoingRequest(
32-
req.header.concat([new HeaderField(AUTHORIZATION, 'Bearer $apiKey')]),
33-
req.body
34-
));
35-
}
42+
public function request(req:OutgoingRequest):Promise<IncomingResponse>
43+
return client.request(new OutgoingRequest(req.header.concat([field]), req.body));
3644
}

src/stripe/api/Charges.hx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,5 @@ interface Charges {
2525
?customer:String,
2626
?source:String, // or a hash containing card info
2727
?statement_descriptor:String,
28-
}):ChargeResponse;
29-
}
30-
31-
typedef ChargeResponse = {
32-
>Response,
33-
>Charge,
28+
}):Charge;
3429
}

src/stripe/api/Customers.hx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,5 @@ interface Customers {
1515
// ?metadata:Metadata, // need support in tink_querystring
1616
?shipping:Shipping,
1717
?source:String, // or Card
18-
}):CustomerResponse;
19-
}
20-
21-
typedef CustomerResponse = {
22-
>Response,
23-
>Customer,
18+
}):Customer;
2419
}

src/stripe/api/Stripe.hx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package stripe.api;
22

33
interface Stripe {
4+
45
@:sub('/v1/customers')
56
var customers:Customers;
7+
68
@:sub('/v1/charges')
79
var charges:Charges;
8-
}
9-
10+
11+
12+
}

src/stripe/types/Error.hx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
package stripe.types;
22

33
import tink.json.Value;
4+
using tink.CoreApi;
45

5-
typedef Error = {
6-
type:ErrorType,
7-
?charge:String,
8-
?message:String,
9-
?code:String,
10-
?decline_code:String,
11-
?param:Value,
6+
abstract Error(ErrorData) {
7+
inline function new(v)
8+
this = v;
9+
10+
public static function parse(v:String)
11+
return tink.Json.parse((v:ErrorData)).map(Error.new);
12+
}
13+
14+
typedef ErrorData = {
15+
error: {
16+
type:ErrorType,
17+
?charge:String,
18+
?message:String,
19+
?code:String,
20+
?decline_code:String,
21+
?param:Value,
22+
}
1223
}
1324

1425
@:enum

tests/Playground.hx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package;
22

33
import tink.http.clients.*;
44
import stripe.Stripe;
5+
import stripe.types.*;
56

67
class Playground {
78
static function main() {
@@ -21,7 +22,7 @@ class Playground {
2122
trace(data, '');
2223
case Failure(err):
2324
trace(err);
24-
trace(haxe.Json.parse(err.data));
25+
trace(Error.parse(err.data));
2526
});
2627
}
2728
}

0 commit comments

Comments
 (0)