@kite-sa/sdk 0.6.0 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,70 @@
2
2
 
3
3
  Formerly `@darak-app/sdk`, which is deprecated. Versions before 0.5.0 were published under that name.
4
4
 
5
+ ## 0.9.2
6
+
7
+ - `parseWebhookEvent(header, body, secret)` from `@kite-sa/sdk/webhooks` verifies a delivery and returns its event typed by `type`; it throws `WebhookError` on a bad signature, a non-event body or an unknown type. `WebhookEvent`, `WebhookEventType`, `WEBHOOK_EVENT_TYPES` and the per-type event interfaces (`WebhookListingCreatedEvent`, …) are generated from the API's OpenAPI `webhooks` schemas. The README has a minimal receiver.
8
+ - The pagination helper now yields advertiser records when paging through `GET /advertisers`.
9
+
10
+ ## 0.9.1
11
+
12
+ - Honor HTTP dates in Retry-After responses, including the one-minute wait limit.
13
+
14
+ ## 0.9.0
15
+
16
+ - Resume listing syncs with the last `updated_at` and `updated_after_id` when multiple listings share a timestamp.
17
+ - Retry interrupted HTTP response bodies with the same POST idempotency key.
18
+
19
+ ## 0.8.0
20
+
21
+ - `kite.valuations.askingRange({...})` calls `GET /valuations/asking-range`: the asking-price
22
+ range for a home you describe (not a listing), with `status` `estimate`,
23
+ `range_only` or `insufficient_evidence`, a provisional `confidence` tier and
24
+ up to 15 comparables. 250 units a call; `market` scope (Starter and up).
25
+
26
+ - `kite.listings.contact(id)` calls `GET /listings/{id}/contact`: the
27
+ advertiser's phone and WhatsApp for one listing, or `available: false` with a
28
+ `reason`. Only to contact the advertiser about that listing. It needs the
29
+ `listings:contact` scope, which no plan includes (enabled per organization on
30
+ request, on any plan; never to test keys), and each organization gets
31
+ 200 numbers per day (Riyadh time) across its keys; past that the call fails
32
+ with 429 `contact_daily_cap_exceeded`.
33
+ - The `Listing` type's `advertiser` gains `name`, `name_en`, `company`, `logo`,
34
+ `fal_number`, `cr`, `ad_license` and `contact_available`. The identity fields
35
+ are null unless the ad carries a REGA licence. No listing carries a phone.
36
+
37
+ ## 0.7.1
38
+
39
+ - Docs only: the `furnished` listing filter now documents the API's new
40
+ behaviour. `furnished=false` matches every listing not confirmed furnished,
41
+ including those that don't state it; `furnished=true` is unchanged. Each
42
+ listing's `furnished` field is still `true`, `false` or `null` (unknown).
43
+
44
+ ## 0.7.0
45
+
46
+ - Writes are safe to retry. A POST without an `Idempotency-Key` now gets one,
47
+ made once per call and reused on every retry, so a lost response can no longer
48
+ create a second API key, webhook or export. Pass your own as before
49
+ (`kite.organization.keys.create(body, { "Idempotency-Key": myKey })`); the
50
+ header name is matched case-insensitively. GET requests carry none.
51
+ - `kite.paginate(fetchPage, params)` (also exported as `paginate`) follows the
52
+ cursor of any list endpoint, e.g.
53
+ `kite.paginate((p) => kite.transactions.search(p), { city: "riyadh" })`.
54
+ `listings.searchAll` now uses it.
55
+ - `@kite-sa/sdk/webhooks` exports `verifyWebhookSignature(header, rawBody, secret)`
56
+ for the `Kite-Signature` header: HMAC-SHA256 over `t.body`, constant-time
57
+ compare, a five-minute window, and an array of secrets during a rotation. It is
58
+ a separate entry point because it needs `node:crypto`.
59
+ - New method `projects.availability(id, params)` for `GET /projects/{id}/availability`: a project's unit
60
+ count and source-reported available units over time, as daily, weekly or
61
+ monthly points with the units absorbed since the previous point.
62
+ - New webhook management methods: `organization.webhooks.update()`
63
+ (`POST /organization/webhooks/{webhook_id}/update`), `organization.webhooks.enable()`
64
+ (`.../enable`) and `organization.webhooks.rotateSecret()` (`.../rotate-secret`).
65
+ `organization.webhooks.deliveries()` takes `cursor` and its response has `pagination`.
66
+ - `verifyWebhookSignature` accepts a header with several `v1` entries, which
67
+ deliveries carry while a rotated secret's overlap lasts; any entry may match.
68
+
5
69
  ## 0.6.0
6
70
 
7
71
  - New methods for the endpoints added since 0.5.0:
package/README.md CHANGED
@@ -25,12 +25,15 @@ for await (const listing of kite.listings.searchAll({ city: "jeddah", listing_ty
25
25
  }
26
26
  ```
27
27
 
28
- Get a key at [platform.kite.sa](https://platform.kite.sa). Keys are secret: call the API from your servers, never from browsers.
28
+ Get a key at [platform.kite.sa](https://platform.kite.sa/?utm_source=npm&utm_medium=referral). Keys are secret: call the API from your servers, never from browsers.
29
29
 
30
30
  ## What you get
31
31
 
32
32
  - Types for every parameter and response, generated from the API's OpenAPI spec.
33
33
  - Retries for `429` and `5xx` (and network errors) that honor `Retry-After`. A monthly-quota `429` isn't waited out; it throws.
34
+ - Safe write retries: a POST is sent with an `Idempotency-Key`, generated for you unless you pass one (`kite.organization.keys.create(body, { "Idempotency-Key": myKey })`), and every retry reuses it, so a lost response never creates a second key, webhook or export.
35
+ - `kite.paginate((p) => kite.transactions.search(p), { city: "riyadh" })` follows the cursor of any list endpoint (path arguments go in the closure: `(p) => kite.projects.units(42, p)`); `listings.searchAll` is the same for listings.
36
+ - Webhook verification and typed events from `@kite-sa/sdk/webhooks` (see [Receiving webhooks](#receiving-webhooks)). `verifyWebhookSignature` takes the `Kite-Signature` header, the raw body and your signing secret (or an array of secrets during a rotation), checks the HMAC in constant time and rejects signatures older than five minutes. Node only (it uses `node:crypto`).
34
37
  - `KiteError` with `status`, `code`, `message`, `param` and `requestId`. Branch on `code`; quote `requestId` to support.
35
38
 
36
39
  - Namespaces generated from the API's OpenAPI spec, so they track it exactly:
@@ -48,3 +51,42 @@ const projects = await kite.projects.search({ city: "riyadh" });
48
51
  0.1.x names for what is now `reference`.
49
52
 
50
53
  Options: `new Kite({ apiKey, baseUrl, maxRetries: 2, timeoutMs: 30000, fetch })`. Needs Node 18+ (or any runtime with `fetch`).
54
+
55
+ ## Receiving webhooks
56
+
57
+ `parseWebhookEvent` verifies a delivery and returns its event, typed by `type` (`WebhookEvent`, generated from the API's OpenAPI `webhooks` schemas). It throws `WebhookError` on a bad signature, a body that isn't an event, or an event type newer than this SDK. Pass the raw body, before any JSON parsing.
58
+
59
+ ```ts
60
+ import { parseWebhookEvent, WebhookError } from "@kite-sa/sdk/webhooks";
61
+
62
+ export async function POST(req: Request): Promise<Response> {
63
+ const body = await req.text(); // the raw body: the signature covers it byte for byte
64
+ let event;
65
+ try {
66
+ event = parseWebhookEvent(
67
+ req.headers.get("Kite-Signature"),
68
+ body,
69
+ process.env.KITE_WEBHOOK_SECRET!,
70
+ );
71
+ } catch (e) {
72
+ if (e instanceof WebhookError) return new Response(null, { status: 400 }); // log e.reason
73
+ throw e;
74
+ }
75
+ if (event.test) return new Response(null, { status: 204 }); // from "Send test event"
76
+ if (await alreadySeen(event.id)) return new Response(null, { status: 204 }); // = Kite-Event-Id
77
+ switch (event.type) {
78
+ case "listing.created":
79
+ await save(event.data);
80
+ break;
81
+ case "listing.price_changed":
82
+ await reprice(event.data.listing_id, event.data.price_yearly_sar);
83
+ break;
84
+ case "listing.delisted":
85
+ await remove(event.data.listing_id);
86
+ break;
87
+ }
88
+ return new Response(null, { status: 204 }); // any 2xx accepts; anything else is retried
89
+ }
90
+ ```
91
+
92
+ Deliveries are at least once, so deduplicate on `event.id` (the `Kite-Event-Id` header). Test events carry `test: true`.