@cords/sdk 0.0.11 → 0.0.13

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.js CHANGED
@@ -79,6 +79,11 @@ var CordsAPI = ({
79
79
  }
80
80
  return res;
81
81
  };
82
+ const formatUrl = (pathname) => {
83
+ const url = new URL(baseUrl);
84
+ url.pathname = url.pathname.replace(/\/$/, "") + pathname;
85
+ return url;
86
+ };
82
87
  const search = async (q, _a) => {
83
88
  var _b = _a, {
84
89
  calculateCityFromSearchString = true,
@@ -87,8 +92,7 @@ var CordsAPI = ({
87
92
  "calculateCityFromSearchString",
88
93
  "calculateProvinceFromSearchString"
89
94
  ]);
90
- const url = new URL(baseUrl);
91
- url.pathname += "/search";
95
+ const url = formatUrl("/search");
92
96
  const params = new URLSearchParams({
93
97
  q
94
98
  });
@@ -107,12 +111,14 @@ var CordsAPI = ({
107
111
  "calculateCityFromSearchString",
108
112
  calculateCityFromSearchString ? "true" : "false"
109
113
  );
110
- if (options.partner) {
114
+ if (options.partner && // Don't add if all values are true (defaults to all)
115
+ !Object.values(options.partner).every((value) => value === true)) {
111
116
  for (const [key, value] of Object.entries(options.partner)) {
112
117
  params.append(`filter[${key}]`, value ? "true" : "false");
113
118
  }
114
119
  }
115
- if (options.delivery) {
120
+ if (options.delivery && // Don't add if all values are true (defaults to all)
121
+ !Object.values(options.delivery).every((value) => value === true)) {
116
122
  for (const [key, value] of Object.entries(options.delivery)) {
117
123
  params.append(
118
124
  `filter[delivery][${key}]`,
@@ -125,8 +131,7 @@ var CordsAPI = ({
125
131
  return data;
126
132
  };
127
133
  const related = async (id) => {
128
- const url = new URL(baseUrl);
129
- url.pathname += `/resource/${id}/related`;
134
+ const url = formatUrl(`/resource/${id}/related`);
130
135
  const res = await request(url.toString());
131
136
  if (!res.ok) {
132
137
  const data2 = await res.json();
@@ -136,8 +141,7 @@ var CordsAPI = ({
136
141
  return data;
137
142
  };
138
143
  const resource = async (id) => {
139
- const url = new URL(baseUrl);
140
- url.pathname += `/resource/${id}`;
144
+ const url = formatUrl(`/resource/${id}`);
141
145
  const res = await request(url.toString());
142
146
  if (!res.ok) {
143
147
  const data2 = await res.json();
@@ -153,16 +157,14 @@ var CordsAPI = ({
153
157
  };
154
158
  const params = new URLSearchParams();
155
159
  ids.forEach((id, index) => params.append(`ids[${index}]`, id));
156
- const url = new URL(baseUrl);
157
- url.pathname += "/resource/list";
160
+ const url = formatUrl("/resource/list");
158
161
  url.search = params.toString();
159
162
  const res = await request(url.toString());
160
163
  const data = await res.json();
161
164
  return data;
162
165
  };
163
166
  const nearestNeighbour = async (id, options) => {
164
- const url = new URL(baseUrl);
165
- url.pathname += `/resource/${id}/nearest-neighbor`;
167
+ const url = formatUrl(`/resource/${id}/nearest-neighbor`);
166
168
  const params = new URLSearchParams({
167
169
  lat: options.lat.toString(),
168
170
  lng: options.lng.toString()
package/dist/index.mjs CHANGED
@@ -56,6 +56,11 @@ var CordsAPI = ({
56
56
  }
57
57
  return res;
58
58
  };
59
+ const formatUrl = (pathname) => {
60
+ const url = new URL(baseUrl);
61
+ url.pathname = url.pathname.replace(/\/$/, "") + pathname;
62
+ return url;
63
+ };
59
64
  const search = async (q, _a) => {
60
65
  var _b = _a, {
61
66
  calculateCityFromSearchString = true,
@@ -64,8 +69,7 @@ var CordsAPI = ({
64
69
  "calculateCityFromSearchString",
65
70
  "calculateProvinceFromSearchString"
66
71
  ]);
67
- const url = new URL(baseUrl);
68
- url.pathname += "/search";
72
+ const url = formatUrl("/search");
69
73
  const params = new URLSearchParams({
70
74
  q
71
75
  });
@@ -84,12 +88,14 @@ var CordsAPI = ({
84
88
  "calculateCityFromSearchString",
85
89
  calculateCityFromSearchString ? "true" : "false"
86
90
  );
87
- if (options.partner) {
91
+ if (options.partner && // Don't add if all values are true (defaults to all)
92
+ !Object.values(options.partner).every((value) => value === true)) {
88
93
  for (const [key, value] of Object.entries(options.partner)) {
89
94
  params.append(`filter[${key}]`, value ? "true" : "false");
90
95
  }
91
96
  }
92
- if (options.delivery) {
97
+ if (options.delivery && // Don't add if all values are true (defaults to all)
98
+ !Object.values(options.delivery).every((value) => value === true)) {
93
99
  for (const [key, value] of Object.entries(options.delivery)) {
94
100
  params.append(
95
101
  `filter[delivery][${key}]`,
@@ -102,8 +108,7 @@ var CordsAPI = ({
102
108
  return data;
103
109
  };
104
110
  const related = async (id) => {
105
- const url = new URL(baseUrl);
106
- url.pathname += `/resource/${id}/related`;
111
+ const url = formatUrl(`/resource/${id}/related`);
107
112
  const res = await request(url.toString());
108
113
  if (!res.ok) {
109
114
  const data2 = await res.json();
@@ -113,8 +118,7 @@ var CordsAPI = ({
113
118
  return data;
114
119
  };
115
120
  const resource = async (id) => {
116
- const url = new URL(baseUrl);
117
- url.pathname += `/resource/${id}`;
121
+ const url = formatUrl(`/resource/${id}`);
118
122
  const res = await request(url.toString());
119
123
  if (!res.ok) {
120
124
  const data2 = await res.json();
@@ -130,16 +134,14 @@ var CordsAPI = ({
130
134
  };
131
135
  const params = new URLSearchParams();
132
136
  ids.forEach((id, index) => params.append(`ids[${index}]`, id));
133
- const url = new URL(baseUrl);
134
- url.pathname += "/resource/list";
137
+ const url = formatUrl("/resource/list");
135
138
  url.search = params.toString();
136
139
  const res = await request(url.toString());
137
140
  const data = await res.json();
138
141
  return data;
139
142
  };
140
143
  const nearestNeighbour = async (id, options) => {
141
- const url = new URL(baseUrl);
142
- url.pathname += `/resource/${id}/nearest-neighbor`;
144
+ const url = formatUrl(`/resource/${id}/nearest-neighbor`);
143
145
  const params = new URLSearchParams({
144
146
  lat: options.lat.toString(),
145
147
  lng: options.lng.toString()
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.11",
6
+ "version": "0.0.13",
7
7
  "private": false,
8
8
  "publishConfig": {
9
9
  "access": "public"
package/src/index.ts CHANGED
@@ -49,6 +49,12 @@ export const CordsAPI = ({
49
49
  return res;
50
50
  };
51
51
 
52
+ const formatUrl = (pathname: string) => {
53
+ const url = new URL(baseUrl);
54
+ url.pathname = url.pathname.replace(/\/$/, "") + pathname;
55
+ return url;
56
+ };
57
+
52
58
  // Search for resources
53
59
  const search = async (
54
60
  q: string,
@@ -58,8 +64,7 @@ export const CordsAPI = ({
58
64
  ...options
59
65
  }: SearchOptions,
60
66
  ) => {
61
- const url = new URL(baseUrl);
62
- url.pathname += "/search";
67
+ const url = formatUrl("/search");
63
68
  const params = new URLSearchParams({
64
69
  q,
65
70
  });
@@ -85,14 +90,22 @@ export const CordsAPI = ({
85
90
  );
86
91
 
87
92
  // Add partner parameters
88
- if (options.partner) {
93
+ if (
94
+ options.partner &&
95
+ // Don't add if all values are true (defaults to all)
96
+ !Object.values(options.partner).every((value) => value === true)
97
+ ) {
89
98
  for (const [key, value] of Object.entries(options.partner)) {
90
99
  params.append(`filter[${key}]`, value ? "true" : "false");
91
100
  }
92
101
  }
93
102
 
94
103
  // Add delivery parameters
95
- if (options.delivery) {
104
+ if (
105
+ options.delivery &&
106
+ // Don't add if all values are true (defaults to all)
107
+ !Object.values(options.delivery).every((value) => value === true)
108
+ ) {
96
109
  for (const [key, value] of Object.entries(options.delivery)) {
97
110
  params.append(
98
111
  `filter[delivery][${key}]`,
@@ -111,8 +124,7 @@ export const CordsAPI = ({
111
124
 
112
125
  // Get related to a resource
113
126
  const related = async (id: string) => {
114
- const url = new URL(baseUrl);
115
- url.pathname += `/resource/${id}/related`;
127
+ const url = formatUrl(`/resource/${id}/related`);
116
128
 
117
129
  const res = await request(url.toString());
118
130
  if (!res.ok) {
@@ -125,8 +137,7 @@ export const CordsAPI = ({
125
137
 
126
138
  // Get a single resource by id
127
139
  const resource = async (id: string) => {
128
- const url = new URL(baseUrl);
129
- url.pathname += `/resource/${id}`;
140
+ const url = formatUrl(`/resource/${id}`);
130
141
 
131
142
  const res = await request(url.toString());
132
143
  if (!res.ok) {
@@ -148,8 +159,7 @@ export const CordsAPI = ({
148
159
  const params = new URLSearchParams();
149
160
  ids.forEach((id, index) => params.append(`ids[${index}]`, id));
150
161
 
151
- const url = new URL(baseUrl);
152
- url.pathname += "/resource/list";
162
+ const url = formatUrl("/resource/list");
153
163
  url.search = params.toString();
154
164
 
155
165
  const res = await request(url.toString());
@@ -165,8 +175,7 @@ export const CordsAPI = ({
165
175
  lng: number;
166
176
  },
167
177
  ) => {
168
- const url = new URL(baseUrl);
169
- url.pathname += `/resource/${id}/nearest-neighbor`;
178
+ const url = formatUrl(`/resource/${id}/nearest-neighbor`);
170
179
 
171
180
  const params = new URLSearchParams({
172
181
  lat: options.lat.toString(),