@kite-sa/sdk 0.5.0 → 0.7.0

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/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { type Endpoints } from "./generated.js";
2
2
  import type { operations } from "./openapi.js";
3
+ import { type Page } from "./paginate.js";
3
4
  export type { operations } from "./openapi.js";
4
5
  export type { Endpoints } from "./generated.js";
6
+ export { paginate, type Page } from "./paginate.js";
5
7
  type Op = keyof operations;
6
8
  type QueryOf<K extends Op> = operations[K]["parameters"] extends {
7
9
  query?: infer Q;
@@ -34,7 +36,11 @@ export interface KiteOptions {
34
36
  /** Defaults to the KITE_API_KEY environment variable. */
35
37
  apiKey?: string;
36
38
  baseUrl?: string;
37
- /** Retries for 429, 5xx and network errors (default 2). */
39
+ /**
40
+ * Retries for 429, 5xx and network errors (default 2). A POST is retried
41
+ * under one Idempotency-Key, which the client generates when you pass none,
42
+ * so a retry can never run a create twice.
43
+ */
38
44
  maxRetries?: number;
39
45
  /** Per-attempt timeout in milliseconds (default 30000). */
40
46
  timeoutMs?: number;
@@ -43,7 +49,7 @@ export interface KiteOptions {
43
49
  }
44
50
  type Query = Record<string, string | number | boolean | readonly (string | number)[] | undefined>;
45
51
  /** Kept in step with package.json by src/index.test.ts. */
46
- export declare const VERSION = "0.5.0";
52
+ export declare const VERSION = "0.7.0";
47
53
  /** Arrays go comma-separated, as the API expects; unset values are dropped. */
48
54
  export declare function toSearchParams(query?: Query): string;
49
55
  /**
@@ -67,7 +73,25 @@ export declare class Kite {
67
73
  private readonly timeoutMs;
68
74
  private readonly fetchImpl;
69
75
  constructor(options?: KiteOptions);
70
- /** Low-level call for any endpoint; prefer the typed helpers below. */
76
+ /**
77
+ * Follows `pagination.next_cursor` for any cursor-paginated list endpoint and
78
+ * yields every item, fetching pages as you iterate:
79
+ *
80
+ * for await (const t of kite.paginate((p) => kite.transactions.search(p), { city: "riyadh" })) {}
81
+ *
82
+ * Path arguments go in the closure: `(p) => kite.projects.units(42, p)`.
83
+ * Stops at the end of the results or at your plan's paging depth.
84
+ */
85
+ paginate<P extends {
86
+ cursor?: string;
87
+ }, T>(fetchPage: (params: P) => Promise<Page<T>>, params: Omit<P, "cursor">): AsyncGenerator<T>;
88
+ /**
89
+ * Low-level call for any endpoint; prefer the typed helpers below.
90
+ *
91
+ * A POST without an `Idempotency-Key` header gets a fresh one, made once here
92
+ * and sent again on every retry, so a lost response can't run the write twice
93
+ * (a second API key whose secret the caller never saw).
94
+ */
71
95
  request<T>(method: "GET" | "POST", path: string, options?: {
72
96
  query?: Query;
73
97
  body?: unknown;
package/dist/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import { createEndpoints } from "./generated.js";
2
+ import { paginate } from "./paginate.js";
3
+ export { paginate } from "./paginate.js";
2
4
  // ── Errors ────────────────────────────────────────────────────────────────
3
5
  /** Any non-2xx response. Branch on `code`; `message` is for humans. */
4
6
  export class KiteError extends Error {
@@ -22,7 +24,7 @@ export class KiteError extends Error {
22
24
  }
23
25
  }
24
26
  /** Kept in step with package.json by src/index.test.ts. */
25
- export const VERSION = "0.5.0";
27
+ export const VERSION = "0.7.0";
26
28
  const USER_AGENT = `kite-sa-js/${VERSION}`;
27
29
  /** Longest Retry-After worth waiting for; anything longer (a monthly quota) throws. */
28
30
  const MAX_RETRY_WAIT_S = 60;
@@ -59,16 +61,7 @@ export class Kite {
59
61
  Object.assign(this, endpoints);
60
62
  this.listings = {
61
63
  ...endpoints.listings,
62
- searchAll: async function* (params) {
63
- let cursor;
64
- do {
65
- const page = await this.listings.search({ ...params, cursor });
66
- yield* page.data;
67
- cursor = page.pagination.has_more
68
- ? (page.pagination.next_cursor ?? undefined)
69
- : undefined;
70
- } while (cursor);
71
- }.bind(this),
64
+ searchAll: (params) => paginate((p) => this.listings.search(p), params),
72
65
  };
73
66
  this.cities = {
74
67
  list: endpoints.reference.cities,
@@ -76,9 +69,30 @@ export class Kite {
76
69
  directions: endpoints.reference.directions,
77
70
  };
78
71
  }
79
- /** Low-level call for any endpoint; prefer the typed helpers below. */
72
+ /**
73
+ * Follows `pagination.next_cursor` for any cursor-paginated list endpoint and
74
+ * yields every item, fetching pages as you iterate:
75
+ *
76
+ * for await (const t of kite.paginate((p) => kite.transactions.search(p), { city: "riyadh" })) {}
77
+ *
78
+ * Path arguments go in the closure: `(p) => kite.projects.units(42, p)`.
79
+ * Stops at the end of the results or at your plan's paging depth.
80
+ */
81
+ paginate(fetchPage, params) {
82
+ return paginate(fetchPage, params);
83
+ }
84
+ /**
85
+ * Low-level call for any endpoint; prefer the typed helpers below.
86
+ *
87
+ * A POST without an `Idempotency-Key` header gets a fresh one, made once here
88
+ * and sent again on every retry, so a lost response can't run the write twice
89
+ * (a second API key whose secret the caller never saw).
90
+ */
80
91
  async request(method, path, options = {}) {
81
92
  const url = `${this.baseUrl}${path}${toSearchParams(options.query)}`;
93
+ const callerHeaders = Object.fromEntries(Object.entries(options.headers ?? {}).filter((entry) => entry[1] !== undefined));
94
+ const hasKey = Object.keys(callerHeaders).some((k) => k.toLowerCase() === "idempotency-key");
95
+ const idempotency = method === "POST" && !hasKey ? { "Idempotency-Key": newIdempotencyKey() } : {};
82
96
  for (let attempt = 0;; attempt++) {
83
97
  let res;
84
98
  try {
@@ -86,7 +100,8 @@ export class Kite {
86
100
  method,
87
101
  headers: {
88
102
  // Caller-supplied optional headers cannot replace the SDK key.
89
- ...Object.fromEntries(Object.entries(options.headers ?? {}).filter((entry) => entry[1] !== undefined)),
103
+ ...callerHeaders,
104
+ ...idempotency,
90
105
  Authorization: `Bearer ${this.apiKey}`,
91
106
  Accept: "application/json",
92
107
  // Browsers forbid setting User-Agent, and the request fails outright
@@ -132,6 +147,15 @@ export class Kite {
132
147
  return this.reference.enums();
133
148
  }
134
149
  }
150
+ function newIdempotencyKey() {
151
+ const c = globalThis.crypto;
152
+ if (c?.randomUUID)
153
+ return c.randomUUID();
154
+ // Node 18 has no global `crypto` without a flag. Keys are scoped to one API key,
155
+ // so collision resistance, not unpredictability, is what matters here.
156
+ const rand = () => Math.random().toString(36).slice(2);
157
+ return `${Date.now().toString(36)}-${rand()}-${rand()}`;
158
+ }
135
159
  /** Exponential backoff with jitter, in milliseconds: ~0.5s, 1s, 2s, … */
136
160
  function backoff(attempt) {
137
161
  return Math.min(8000, 500 * 2 ** attempt) * (0.75 + Math.random() * 0.5);
@@ -96,6 +96,42 @@ export declare function referenceLimits(config?: Partial<ClientConfig>): Promise
96
96
  max_result_depth: number;
97
97
  };
98
98
  }>;
99
+ /**
100
+ * @throws {ApiError<400, Error>}
101
+ * @throws {ApiError<401, Error>}
102
+ * @throws {ApiError<403, Error>}
103
+ * @throws {ApiError<429, Error>}
104
+ * @throws {ApiError<500, Error>}
105
+ */
106
+ export declare function referenceCoverage(params?: {
107
+ city?: string;
108
+ }, config?: Partial<ClientConfig>): Promise<{
109
+ data: {
110
+ computed_at: string;
111
+ cities: {
112
+ city: string;
113
+ name_en: string;
114
+ name_ar: string;
115
+ listings: {
116
+ listing_type: string;
117
+ property_family: string;
118
+ property_types: string[];
119
+ active_listings: number;
120
+ neighborhoods_with_10_listings: number;
121
+ price_band_ready_pct: number | null;
122
+ }[];
123
+ registered_deals: {
124
+ latest_quarter: string | null;
125
+ neighborhood_mapped_pct: number | null;
126
+ mappable_deals: number;
127
+ };
128
+ registered_rents: {
129
+ available: boolean;
130
+ latest_quarter: string | null;
131
+ };
132
+ }[];
133
+ };
134
+ }>;
99
135
  /**
100
136
  * @throws {ApiError<400, Error>}
101
137
  * @throws {ApiError<401, Error>}
@@ -128,6 +164,7 @@ export declare function listingsSearch(params: {
128
164
  q?: string;
129
165
  amenities?: string[];
130
166
  amenitiesExclude?: string[];
167
+ photoFeatures?: string[];
131
168
  floor?: "ground" | "upper";
132
169
  sourceExclude?: string[];
133
170
  maxAge?: number;
@@ -177,6 +214,7 @@ export declare function listingsCount(params: {
177
214
  q?: string;
178
215
  amenities?: string[];
179
216
  amenitiesExclude?: string[];
217
+ photoFeatures?: string[];
180
218
  floor?: "ground" | "upper";
181
219
  sourceExclude?: string[];
182
220
  maxAge?: number;
@@ -535,6 +573,82 @@ export declare function marketNeighborhoods(params: {
535
573
  };
536
574
  };
537
575
  }>;
576
+ /**
577
+ * @throws {ApiError<400, Error>}
578
+ * @throws {ApiError<401, Error>}
579
+ * @throws {ApiError<403, Error>}
580
+ * @throws {ApiError<429, Error>}
581
+ * @throws {ApiError<500, Error>}
582
+ */
583
+ export declare function marketCommercialRents(params: {
584
+ city: string;
585
+ propertyType?: string[];
586
+ neighborhoodId?: number[];
587
+ areaMin?: number;
588
+ areaMax?: number;
589
+ minSampleSize?: number;
590
+ }, config?: Partial<ClientConfig>): Promise<{
591
+ data: {
592
+ filters: {
593
+ city: string;
594
+ listing_type: string;
595
+ listing_category: string;
596
+ property_types: string[];
597
+ neighborhood_ids: unknown;
598
+ area_min: unknown;
599
+ area_max: unknown;
600
+ };
601
+ price_basis: string;
602
+ price_source: string;
603
+ min_sample_size: number;
604
+ city_totals: {
605
+ property_type: string;
606
+ sample_size: number;
607
+ median_rent_per_sqm_sar: unknown;
608
+ p25_rent_per_sqm_sar: unknown;
609
+ p75_rent_per_sqm_sar: unknown;
610
+ median_area_sqm: unknown;
611
+ median_rent_sar: unknown;
612
+ street_width: {
613
+ sample_size: number;
614
+ median_m: number | null;
615
+ };
616
+ exclusions: {
617
+ listings_in_scope: number;
618
+ lease_takeover: number;
619
+ implausible_area_or_price: number;
620
+ repeated_listing: number;
621
+ outside_price_per_sqm_fence: number;
622
+ };
623
+ price_per_sqm_fence_sar: {
624
+ lower: number;
625
+ upper: unknown;
626
+ };
627
+ }[];
628
+ neighborhoods: {
629
+ neighborhood_id: number;
630
+ name_ar: string;
631
+ name_en: string;
632
+ center: unknown;
633
+ property_type: string;
634
+ sample_size: number;
635
+ median_rent_per_sqm_sar: unknown;
636
+ p25_rent_per_sqm_sar: unknown;
637
+ p75_rent_per_sqm_sar: unknown;
638
+ median_area_sqm: unknown;
639
+ median_rent_sar: unknown;
640
+ street_width: {
641
+ sample_size: number;
642
+ median_m: number | null;
643
+ };
644
+ }[];
645
+ window: {
646
+ updated_within_days: number;
647
+ from: string;
648
+ to: string;
649
+ };
650
+ };
651
+ }>;
538
652
  /**
539
653
  * @throws {ApiError<400, Error>}
540
654
  * @throws {ApiError<401, Error>}
@@ -766,6 +880,41 @@ export declare function analyticsListingPriceBand(id: number, config?: Partial<C
766
880
  * @throws {ApiError<429, Error>}
767
881
  * @throws {ApiError<500, Error>}
768
882
  */
883
+ export declare function analyticsBelowMarket(params: {
884
+ city: string;
885
+ listingType: "rent" | "sale";
886
+ propertyType?: "apartment" | "villa" | "floor" | "duplex";
887
+ beds?: number;
888
+ neighborhoodId?: number[];
889
+ minDiscountPct?: number;
890
+ maxDiscountPct?: number;
891
+ limit?: number;
892
+ cursor?: string;
893
+ }, config?: Partial<ClientConfig>): Promise<{
894
+ data: {
895
+ listing: Listing;
896
+ comparison: {
897
+ median_price_sar: unknown;
898
+ p25_price_sar: unknown;
899
+ p75_price_sar: unknown;
900
+ percent_vs_median: number | null;
901
+ sample_size: number;
902
+ radius_m: unknown;
903
+ look_back_days: unknown;
904
+ price_basis: string;
905
+ price_source: string;
906
+ };
907
+ }[];
908
+ pagination: Pagination;
909
+ }>;
910
+ /**
911
+ * @deprecated
912
+ * @throws {ApiError<400, Error>}
913
+ * @throws {ApiError<401, Error>}
914
+ * @throws {ApiError<403, Error>}
915
+ * @throws {ApiError<429, Error>}
916
+ * @throws {ApiError<500, Error>}
917
+ */
769
918
  export declare function analyticsDeals(params: {
770
919
  city: string;
771
920
  listingType: "rent" | "sale";
@@ -787,6 +936,7 @@ export declare function analyticsDeals(params: {
787
936
  sample_size: number;
788
937
  radius_m: unknown;
789
938
  look_back_days: unknown;
939
+ price_basis: string;
790
940
  price_source: string;
791
941
  };
792
942
  }[];
@@ -874,12 +1024,17 @@ export declare function analyticsTrends(params: {
874
1024
  name_en: string | null;
875
1025
  name_ar: string | null;
876
1026
  change_pct: number | null;
1027
+ index_change_pct: number | null;
1028
+ index_base_month: string | null;
877
1029
  points: {
878
1030
  month: string;
879
1031
  sample_size: number;
880
1032
  median_price_sar: unknown;
881
1033
  p25_price_sar: unknown;
882
1034
  p75_price_sar: unknown;
1035
+ price_index: number | null;
1036
+ index_cells: number;
1037
+ index_sample_size: number;
883
1038
  partial: boolean;
884
1039
  coverage: string;
885
1040
  }[];
@@ -979,6 +1134,7 @@ export declare function transactionsSearch(params: {
979
1134
  }, config?: Partial<ClientConfig>): Promise<{
980
1135
  data: Transaction[];
981
1136
  pagination: Pagination;
1137
+ price_basis: string;
982
1138
  price_source: string;
983
1139
  coverage: TransactionCoverage;
984
1140
  attribution: TransactionAttribution;
@@ -1006,6 +1162,7 @@ export declare function marketTransactions(params: {
1006
1162
  group_by: string;
1007
1163
  land_use: unknown;
1008
1164
  };
1165
+ price_basis: string;
1009
1166
  price_source: string;
1010
1167
  window: {
1011
1168
  quarter_from: string;
@@ -1044,6 +1201,7 @@ export declare function marketTransactions(params: {
1044
1201
  export declare function marketLandPrices(params: {
1045
1202
  city: string;
1046
1203
  neighborhoodId?: number[];
1204
+ groupBy?: "city" | "neighborhood";
1047
1205
  landUse?: "residential" | "commercial";
1048
1206
  quarterFrom?: string;
1049
1207
  quarterTo?: string;
@@ -1053,9 +1211,11 @@ export declare function marketLandPrices(params: {
1053
1211
  filters: {
1054
1212
  city: string;
1055
1213
  neighborhood_ids: unknown;
1214
+ group_by: string;
1056
1215
  land_use: string;
1057
1216
  };
1058
1217
  asset: string;
1218
+ price_basis: string;
1059
1219
  price_source: string;
1060
1220
  window: {
1061
1221
  quarter_from: string;
@@ -1282,6 +1442,43 @@ export declare function projectsUnits(id: number, params?: {
1282
1442
  }[];
1283
1443
  pagination: Pagination;
1284
1444
  }>;
1445
+ /**
1446
+ * @throws {ApiError<400, Error>}
1447
+ * @throws {ApiError<401, Error>}
1448
+ * @throws {ApiError<403, Error>}
1449
+ * @throws {ApiError<404, Error>}
1450
+ * @throws {ApiError<429, Error>}
1451
+ * @throws {ApiError<500, Error>}
1452
+ */
1453
+ export declare function projectsAvailability(id: number, params?: {
1454
+ interval?: "daily" | "weekly" | "monthly";
1455
+ from?: string;
1456
+ to?: string;
1457
+ unitType?: string;
1458
+ }, config?: Partial<ClientConfig>): Promise<{
1459
+ data: {
1460
+ project_id: number;
1461
+ interval: string;
1462
+ from: string;
1463
+ to: string;
1464
+ unit_type: string | null;
1465
+ coverage: {
1466
+ first_snapshot: string | null;
1467
+ last_snapshot: string | null;
1468
+ snapshots: number;
1469
+ };
1470
+ points: {
1471
+ date: string;
1472
+ project_status: string | null;
1473
+ total_units: unknown;
1474
+ available_units: unknown;
1475
+ absorbed_since_previous: unknown;
1476
+ unit_types: unknown;
1477
+ starting_price: unknown;
1478
+ source: string;
1479
+ }[];
1480
+ };
1481
+ }>;
1285
1482
  /**
1286
1483
  * @throws {ApiError<400, Error>}
1287
1484
  * @throws {ApiError<401, Error>}
@@ -1668,6 +1865,91 @@ export declare function organizationWebhooksCreate(body: {
1668
1865
  secret: string;
1669
1866
  };
1670
1867
  }>;
1868
+ /**
1869
+ * @throws {ApiError<400, Error>}
1870
+ * @throws {ApiError<401, Error>}
1871
+ * @throws {ApiError<403, Error>}
1872
+ * @throws {ApiError<404, Error>}
1873
+ * @throws {ApiError<409, Error>}
1874
+ * @throws {ApiError<429, Error>}
1875
+ * @throws {ApiError<500, Error>}
1876
+ */
1877
+ export declare function organizationWebhooksUpdate(webhookId: string, body: {
1878
+ url?: string;
1879
+ event_types?: string[];
1880
+ filters?: {
1881
+ city: string;
1882
+ listing_type?: string;
1883
+ listing_category?: string;
1884
+ property_type?: string;
1885
+ neighborhood?: string;
1886
+ beds?: string;
1887
+ beds_min?: string;
1888
+ price_min?: string;
1889
+ price_max?: string;
1890
+ area_min?: string;
1891
+ area_max?: string;
1892
+ };
1893
+ description?: unknown;
1894
+ }, params?: {
1895
+ idempotencyKey?: string;
1896
+ }, config?: Partial<ClientConfig>): Promise<{
1897
+ data: {
1898
+ id: string;
1899
+ url: string;
1900
+ event_types: string[];
1901
+ filters: Record<string, unknown>;
1902
+ description: string | null;
1903
+ status: string;
1904
+ disabled_reason: string | null;
1905
+ consecutive_failures: number;
1906
+ created_at: string;
1907
+ };
1908
+ }>;
1909
+ /**
1910
+ * @throws {ApiError<400, Error>}
1911
+ * @throws {ApiError<401, Error>}
1912
+ * @throws {ApiError<403, Error>}
1913
+ * @throws {ApiError<404, Error>}
1914
+ * @throws {ApiError<409, Error>}
1915
+ * @throws {ApiError<429, Error>}
1916
+ * @throws {ApiError<500, Error>}
1917
+ */
1918
+ export declare function organizationWebhooksEnable(webhookId: string, params?: {
1919
+ idempotencyKey?: string;
1920
+ }, config?: Partial<ClientConfig>): Promise<{
1921
+ data: {
1922
+ id: string;
1923
+ url: string;
1924
+ event_types: string[];
1925
+ filters: Record<string, unknown>;
1926
+ description: string | null;
1927
+ status: string;
1928
+ disabled_reason: string | null;
1929
+ consecutive_failures: number;
1930
+ created_at: string;
1931
+ };
1932
+ }>;
1933
+ /**
1934
+ * @throws {ApiError<400, Error>}
1935
+ * @throws {ApiError<401, Error>}
1936
+ * @throws {ApiError<403, Error>}
1937
+ * @throws {ApiError<404, Error>}
1938
+ * @throws {ApiError<409, Error>}
1939
+ * @throws {ApiError<429, Error>}
1940
+ * @throws {ApiError<500, Error>}
1941
+ */
1942
+ export declare function organizationWebhooksRotateSecret(webhookId: string, body: {
1943
+ overlap_seconds?: number;
1944
+ }, params?: {
1945
+ idempotencyKey?: string;
1946
+ }, config?: Partial<ClientConfig>): Promise<{
1947
+ data: {
1948
+ id: string;
1949
+ secret: string;
1950
+ previous_secret_expires_at: unknown;
1951
+ };
1952
+ }>;
1671
1953
  /**
1672
1954
  * @throws {ApiError<400, Error>}
1673
1955
  * @throws {ApiError<401, Error>}
@@ -1695,6 +1977,7 @@ export declare function organizationWebhooksDelete(webhookId: string, params?: {
1695
1977
  */
1696
1978
  export declare function organizationWebhooksDeliveries(webhookId: string, params?: {
1697
1979
  limit?: number;
1980
+ cursor?: string;
1698
1981
  }, config?: Partial<ClientConfig>): Promise<{
1699
1982
  data: {
1700
1983
  id: number;
@@ -1708,6 +1991,7 @@ export declare function organizationWebhooksDeliveries(webhookId: string, params
1708
1991
  delivered_at: unknown;
1709
1992
  created_at: string;
1710
1993
  }[];
1994
+ pagination: Pagination;
1711
1995
  }>;
1712
1996
  /**
1713
1997
  * @throws {ApiError<400, Error>}