@cords/sdk 0.0.8 → 0.0.9

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