@cords/sdk 0.0.8 → 0.0.10

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +38 -17
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "main": "./dist/index.js",
4
4
  "module": "./dist/index.mjs",
5
5
  "types": "./dist/index.d.ts",
6
- "version": "0.0.8",
6
+ "version": "0.0.10",
7
7
  "private": false,
8
8
  "publishConfig": {
9
9
  "access": "public"
package/src/index.ts CHANGED
@@ -13,7 +13,7 @@ export const CordsAPI = ({
13
13
  apiKey,
14
14
  version = "production",
15
15
  referer,
16
- baseUrl,
16
+ baseUrl: customBaseUrl,
17
17
  }: {
18
18
  apiKey: string;
19
19
  version?: "production" | "dev";
@@ -21,7 +21,11 @@ export const CordsAPI = ({
21
21
  baseUrl?: string;
22
22
  }) => {
23
23
  // Set the base URL for the Cords API
24
- baseUrl = baseUrl ?? (version === "production" ? "https://api.cords.ai" : "https://api.cords.dev");
24
+ const baseUrl = customBaseUrl
25
+ ? customBaseUrl?.replace(/\/$/, "")
26
+ : version === "production"
27
+ ? "https://api.cords.ai"
28
+ : "https://api.cords.dev";
25
29
 
26
30
  // Helper for making requests to the Cords API that applies the api key, referrer, and handles errors
27
31
  const request = async (input: RequestInfo, init?: RequestInit) => {
@@ -35,7 +39,9 @@ export const CordsAPI = ({
35
39
  });
36
40
  if (!res.ok) {
37
41
  if (res.status === 403)
38
- throw new Error("Bad API key. Ensure you have a valid API key.");
42
+ throw new Error(
43
+ "Bad API key. Ensure you have a valid API key.",
44
+ );
39
45
  const data: CordsError = await res.json();
40
46
  if (data.detail) throw new Error(data.detail);
41
47
  else throw new Error("An error occurred");
@@ -50,9 +56,10 @@ export const CordsAPI = ({
50
56
  calculateCityFromSearchString = true,
51
57
  calculateProvinceFromSearchString = true,
52
58
  ...options
53
- }: SearchOptions
59
+ }: SearchOptions,
54
60
  ) => {
55
- const url = new URL("/search", baseUrl);
61
+ const url = new URL(baseUrl);
62
+ url.pathname += "/search";
56
63
  const params = new URLSearchParams({
57
64
  q,
58
65
  });
@@ -62,17 +69,19 @@ export const CordsAPI = ({
62
69
 
63
70
  // Add top-level parameters
64
71
  if (options.page) params.append("page", options.page.toString());
65
- if (options.pageSize) params.append("pageSize", options.pageSize.toString());
66
- if (options.distance) params.append("distance", options.distance.toString());
72
+ if (options.pageSize)
73
+ params.append("pageSize", options.pageSize.toString());
74
+ if (options.distance)
75
+ params.append("distance", options.distance.toString());
67
76
 
68
77
  // Add boolean parameters
69
78
  params.append(
70
79
  "calculateProvinceFromSearchString",
71
- calculateProvinceFromSearchString ? "true" : "false"
80
+ calculateProvinceFromSearchString ? "true" : "false",
72
81
  );
73
82
  params.append(
74
83
  "calculateCityFromSearchString",
75
- calculateCityFromSearchString ? "true" : "false"
84
+ calculateCityFromSearchString ? "true" : "false",
76
85
  );
77
86
 
78
87
  // Add partner parameters
@@ -85,7 +94,10 @@ export const CordsAPI = ({
85
94
  // Add delivery parameters
86
95
  if (options.delivery) {
87
96
  for (const [key, value] of Object.entries(options.delivery)) {
88
- params.append(`filter[delivery][${key}]`, value ? "true" : "false");
97
+ params.append(
98
+ `filter[delivery][${key}]`,
99
+ value ? "true" : "false",
100
+ );
89
101
  }
90
102
  }
91
103
 
@@ -99,7 +111,8 @@ export const CordsAPI = ({
99
111
 
100
112
  // Get related to a resource
101
113
  const related = async (id: string) => {
102
- const url = new URL(`/resource/${id}/related`, baseUrl);
114
+ const url = new URL(baseUrl);
115
+ url.pathname += `/resource/${id}/related`;
103
116
 
104
117
  const res = await request(url.toString());
105
118
  if (!res.ok) {
@@ -112,7 +125,8 @@ export const CordsAPI = ({
112
125
 
113
126
  // Get a single resource by id
114
127
  const resource = async (id: string) => {
115
- const url = new URL(`/resource/${id}`, baseUrl);
128
+ const url = new URL(baseUrl);
129
+ url.pathname += `/resource/${id}`;
116
130
 
117
131
  const res = await request(url.toString());
118
132
  if (!res.ok) {
@@ -124,7 +138,9 @@ export const CordsAPI = ({
124
138
  };
125
139
 
126
140
  // Get a list of resources by id
127
- const resourceList = async (ids: string[]): Promise<{ data: ResourceType[] }> => {
141
+ const resourceList = async (
142
+ ids: string[],
143
+ ): Promise<{ data: ResourceType[] }> => {
128
144
  if (ids.length === 0)
129
145
  return {
130
146
  data: [],
@@ -132,7 +148,9 @@ export const CordsAPI = ({
132
148
  const params = new URLSearchParams();
133
149
  ids.forEach((id, index) => params.append(`ids[${index}]`, id));
134
150
 
135
- const url = new URL(`/search?${params.toString()}`, baseUrl);
151
+ const url = new URL(baseUrl);
152
+ url.pathname += "/resource/list";
153
+ url.search = params.toString();
136
154
 
137
155
  const res = await request(url.toString());
138
156
  const data = await res.json();
@@ -145,16 +163,19 @@ export const CordsAPI = ({
145
163
  options: {
146
164
  lat: number;
147
165
  lng: number;
148
- }
166
+ },
149
167
  ) => {
150
- const url = new URL(`/resource/${id}/nearest-neighbor`, baseUrl);
168
+ const url = new URL(baseUrl);
169
+ url.pathname += `/resource/${id}/nearest-neighbor`;
151
170
 
152
171
  const params = new URLSearchParams({
153
172
  lat: options.lat.toString(),
154
173
  lng: options.lng.toString(),
155
174
  });
156
175
 
157
- const res = await request(url.toString() + "?delivery=local&" + params.toString());
176
+ const res = await request(
177
+ url.toString() + "?delivery=local&" + params.toString(),
178
+ );
158
179
  if (!res.ok) {
159
180
  const data: CordsError = await res.json();
160
181
  throw new Error(data.detail);