@cords/sdk 0.0.5 → 0.0.7
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/README.md +1 -1
- package/dist/index.d.mts +12 -3
- package/dist/index.d.ts +12 -3
- package/dist/index.js +38 -16
- package/dist/index.mjs +38 -16
- package/package.json +1 -1
- package/src/index.ts +29 -1
- package/src/types.ts +9 -1
package/README.md
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -53,7 +53,12 @@ type ResourceType = {
|
|
|
53
53
|
vectorDistance: number;
|
|
54
54
|
} | null;
|
|
55
55
|
};
|
|
56
|
-
type SearchResourceType = Omit<ResourceType, "website" | "email" | "phoneNumbers" | "addresses" | "address"
|
|
56
|
+
type SearchResourceType = Omit<ResourceType, "website" | "email" | "phoneNumbers" | "addresses" | "address"> & {
|
|
57
|
+
location: {
|
|
58
|
+
lat: number | null;
|
|
59
|
+
lng: number | null;
|
|
60
|
+
};
|
|
61
|
+
};
|
|
57
62
|
type SearchOptions = {
|
|
58
63
|
lat: number;
|
|
59
64
|
lng: number;
|
|
@@ -65,6 +70,7 @@ type SearchOptions = {
|
|
|
65
70
|
mentor?: boolean;
|
|
66
71
|
prosper?: boolean;
|
|
67
72
|
magnet?: boolean;
|
|
73
|
+
volunteer?: boolean;
|
|
68
74
|
};
|
|
69
75
|
delivery?: {
|
|
70
76
|
local?: boolean;
|
|
@@ -72,6 +78,8 @@ type SearchOptions = {
|
|
|
72
78
|
provincial?: boolean;
|
|
73
79
|
national?: boolean;
|
|
74
80
|
};
|
|
81
|
+
calculateProvinceFromSearchString?: boolean;
|
|
82
|
+
calculateCityFromSearchString?: boolean;
|
|
75
83
|
};
|
|
76
84
|
type CordsError = {
|
|
77
85
|
detail: string;
|
|
@@ -81,11 +89,12 @@ type CordsError = {
|
|
|
81
89
|
};
|
|
82
90
|
|
|
83
91
|
declare const ResourceOptions: {};
|
|
84
|
-
declare const CordsAPI: ({ apiKey, version, }: {
|
|
92
|
+
declare const CordsAPI: ({ apiKey, version, referer, }: {
|
|
85
93
|
apiKey: string;
|
|
86
94
|
version?: "production" | "dev";
|
|
95
|
+
referer?: string;
|
|
87
96
|
}) => {
|
|
88
|
-
search: (q: string, options: SearchOptions) => Promise<{
|
|
97
|
+
search: (q: string, { calculateCityFromSearchString, calculateProvinceFromSearchString, ...options }: SearchOptions) => Promise<{
|
|
89
98
|
data: SearchResourceType[];
|
|
90
99
|
meta: {
|
|
91
100
|
total: number;
|
package/dist/index.d.ts
CHANGED
|
@@ -53,7 +53,12 @@ type ResourceType = {
|
|
|
53
53
|
vectorDistance: number;
|
|
54
54
|
} | null;
|
|
55
55
|
};
|
|
56
|
-
type SearchResourceType = Omit<ResourceType, "website" | "email" | "phoneNumbers" | "addresses" | "address"
|
|
56
|
+
type SearchResourceType = Omit<ResourceType, "website" | "email" | "phoneNumbers" | "addresses" | "address"> & {
|
|
57
|
+
location: {
|
|
58
|
+
lat: number | null;
|
|
59
|
+
lng: number | null;
|
|
60
|
+
};
|
|
61
|
+
};
|
|
57
62
|
type SearchOptions = {
|
|
58
63
|
lat: number;
|
|
59
64
|
lng: number;
|
|
@@ -65,6 +70,7 @@ type SearchOptions = {
|
|
|
65
70
|
mentor?: boolean;
|
|
66
71
|
prosper?: boolean;
|
|
67
72
|
magnet?: boolean;
|
|
73
|
+
volunteer?: boolean;
|
|
68
74
|
};
|
|
69
75
|
delivery?: {
|
|
70
76
|
local?: boolean;
|
|
@@ -72,6 +78,8 @@ type SearchOptions = {
|
|
|
72
78
|
provincial?: boolean;
|
|
73
79
|
national?: boolean;
|
|
74
80
|
};
|
|
81
|
+
calculateProvinceFromSearchString?: boolean;
|
|
82
|
+
calculateCityFromSearchString?: boolean;
|
|
75
83
|
};
|
|
76
84
|
type CordsError = {
|
|
77
85
|
detail: string;
|
|
@@ -81,11 +89,12 @@ type CordsError = {
|
|
|
81
89
|
};
|
|
82
90
|
|
|
83
91
|
declare const ResourceOptions: {};
|
|
84
|
-
declare const CordsAPI: ({ apiKey, version, }: {
|
|
92
|
+
declare const CordsAPI: ({ apiKey, version, referer, }: {
|
|
85
93
|
apiKey: string;
|
|
86
94
|
version?: "production" | "dev";
|
|
95
|
+
referer?: string;
|
|
87
96
|
}) => {
|
|
88
|
-
search: (q: string, options: SearchOptions) => Promise<{
|
|
97
|
+
search: (q: string, { calculateCityFromSearchString, calculateProvinceFromSearchString, ...options }: SearchOptions) => Promise<{
|
|
89
98
|
data: SearchResourceType[];
|
|
90
99
|
meta: {
|
|
91
100
|
total: number;
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,18 @@ var __spreadValues = (a, b) => {
|
|
|
20
20
|
return a;
|
|
21
21
|
};
|
|
22
22
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
23
|
+
var __objRest = (source, exclude) => {
|
|
24
|
+
var target = {};
|
|
25
|
+
for (var prop in source)
|
|
26
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
27
|
+
target[prop] = source[prop];
|
|
28
|
+
if (source != null && __getOwnPropSymbols)
|
|
29
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
30
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
31
|
+
target[prop] = source[prop];
|
|
32
|
+
}
|
|
33
|
+
return target;
|
|
34
|
+
};
|
|
23
35
|
var __export = (target, all) => {
|
|
24
36
|
for (var name in all)
|
|
25
37
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -65,39 +77,50 @@ module.exports = __toCommonJS(src_exports);
|
|
|
65
77
|
var ResourceOptions = {};
|
|
66
78
|
var CordsAPI = ({
|
|
67
79
|
apiKey,
|
|
68
|
-
version = "production"
|
|
80
|
+
version = "production",
|
|
81
|
+
referer
|
|
69
82
|
}) => {
|
|
70
83
|
const baseUrl = version === "production" ? "https://api.cords.ai" : "https://api.cords.dev";
|
|
71
84
|
const request = (input, init) => __async(void 0, null, function* () {
|
|
72
85
|
const res = yield fetch(input, __spreadProps(__spreadValues({}, init), {
|
|
73
|
-
headers: __spreadValues({
|
|
86
|
+
headers: __spreadValues(__spreadValues({
|
|
74
87
|
"x-api-key": apiKey
|
|
75
|
-
}, init == null ? void 0 : init.headers)
|
|
88
|
+
}, referer ? { referer } : {}), init == null ? void 0 : init.headers)
|
|
76
89
|
}));
|
|
77
90
|
if (!res.ok) {
|
|
78
91
|
if (res.status === 403)
|
|
79
92
|
throw new Error("Bad API key. Ensure you have a valid API key.");
|
|
80
93
|
const data = yield res.json();
|
|
81
|
-
if (data.detail)
|
|
82
|
-
|
|
83
|
-
else
|
|
84
|
-
throw new Error("An error occurred");
|
|
94
|
+
if (data.detail) throw new Error(data.detail);
|
|
95
|
+
else throw new Error("An error occurred");
|
|
85
96
|
}
|
|
86
97
|
return res;
|
|
87
98
|
});
|
|
88
|
-
const search = (q,
|
|
99
|
+
const search = (q, _a) => __async(void 0, null, function* () {
|
|
100
|
+
var _b = _a, {
|
|
101
|
+
calculateCityFromSearchString = true,
|
|
102
|
+
calculateProvinceFromSearchString = true
|
|
103
|
+
} = _b, options = __objRest(_b, [
|
|
104
|
+
"calculateCityFromSearchString",
|
|
105
|
+
"calculateProvinceFromSearchString"
|
|
106
|
+
]);
|
|
89
107
|
const url = new URL("/search", baseUrl);
|
|
90
108
|
const params = new URLSearchParams({
|
|
91
109
|
q
|
|
92
110
|
});
|
|
93
111
|
params.append("lat", options.lat.toString());
|
|
94
112
|
params.append("lng", options.lng.toString());
|
|
95
|
-
if (options.page)
|
|
96
|
-
|
|
97
|
-
if (options.
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
113
|
+
if (options.page) params.append("page", options.page.toString());
|
|
114
|
+
if (options.pageSize) params.append("pageSize", options.pageSize.toString());
|
|
115
|
+
if (options.distance) params.append("distance", options.distance.toString());
|
|
116
|
+
params.append(
|
|
117
|
+
"calculateProvinceFromSearchString",
|
|
118
|
+
calculateProvinceFromSearchString ? "true" : "false"
|
|
119
|
+
);
|
|
120
|
+
params.append(
|
|
121
|
+
"calculateCityFromSearchString",
|
|
122
|
+
calculateCityFromSearchString ? "true" : "false"
|
|
123
|
+
);
|
|
101
124
|
if (options.partner) {
|
|
102
125
|
for (const [key, value] of Object.entries(options.partner)) {
|
|
103
126
|
params.append(`filter[${key}]`, value ? "true" : "false");
|
|
@@ -175,8 +198,7 @@ var formatServiceAddress = (address) => {
|
|
|
175
198
|
const newAddress = street1 + street2 + city + province + postalCode;
|
|
176
199
|
if (newAddress.endsWith(", ")) {
|
|
177
200
|
return newAddress.slice(0, -2);
|
|
178
|
-
} else
|
|
179
|
-
return newAddress;
|
|
201
|
+
} else return newAddress;
|
|
180
202
|
};
|
|
181
203
|
// Annotate the CommonJS export names for ESM import in node:
|
|
182
204
|
0 && (module.exports = {
|
package/dist/index.mjs
CHANGED
|
@@ -17,6 +17,18 @@ var __spreadValues = (a, b) => {
|
|
|
17
17
|
return a;
|
|
18
18
|
};
|
|
19
19
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __objRest = (source, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source)
|
|
23
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source[prop];
|
|
25
|
+
if (source != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
20
32
|
var __async = (__this, __arguments, generator) => {
|
|
21
33
|
return new Promise((resolve, reject) => {
|
|
22
34
|
var fulfilled = (value) => {
|
|
@@ -42,39 +54,50 @@ var __async = (__this, __arguments, generator) => {
|
|
|
42
54
|
var ResourceOptions = {};
|
|
43
55
|
var CordsAPI = ({
|
|
44
56
|
apiKey,
|
|
45
|
-
version = "production"
|
|
57
|
+
version = "production",
|
|
58
|
+
referer
|
|
46
59
|
}) => {
|
|
47
60
|
const baseUrl = version === "production" ? "https://api.cords.ai" : "https://api.cords.dev";
|
|
48
61
|
const request = (input, init) => __async(void 0, null, function* () {
|
|
49
62
|
const res = yield fetch(input, __spreadProps(__spreadValues({}, init), {
|
|
50
|
-
headers: __spreadValues({
|
|
63
|
+
headers: __spreadValues(__spreadValues({
|
|
51
64
|
"x-api-key": apiKey
|
|
52
|
-
}, init == null ? void 0 : init.headers)
|
|
65
|
+
}, referer ? { referer } : {}), init == null ? void 0 : init.headers)
|
|
53
66
|
}));
|
|
54
67
|
if (!res.ok) {
|
|
55
68
|
if (res.status === 403)
|
|
56
69
|
throw new Error("Bad API key. Ensure you have a valid API key.");
|
|
57
70
|
const data = yield res.json();
|
|
58
|
-
if (data.detail)
|
|
59
|
-
|
|
60
|
-
else
|
|
61
|
-
throw new Error("An error occurred");
|
|
71
|
+
if (data.detail) throw new Error(data.detail);
|
|
72
|
+
else throw new Error("An error occurred");
|
|
62
73
|
}
|
|
63
74
|
return res;
|
|
64
75
|
});
|
|
65
|
-
const search = (q,
|
|
76
|
+
const search = (q, _a) => __async(void 0, null, function* () {
|
|
77
|
+
var _b = _a, {
|
|
78
|
+
calculateCityFromSearchString = true,
|
|
79
|
+
calculateProvinceFromSearchString = true
|
|
80
|
+
} = _b, options = __objRest(_b, [
|
|
81
|
+
"calculateCityFromSearchString",
|
|
82
|
+
"calculateProvinceFromSearchString"
|
|
83
|
+
]);
|
|
66
84
|
const url = new URL("/search", baseUrl);
|
|
67
85
|
const params = new URLSearchParams({
|
|
68
86
|
q
|
|
69
87
|
});
|
|
70
88
|
params.append("lat", options.lat.toString());
|
|
71
89
|
params.append("lng", options.lng.toString());
|
|
72
|
-
if (options.page)
|
|
73
|
-
|
|
74
|
-
if (options.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
90
|
+
if (options.page) params.append("page", options.page.toString());
|
|
91
|
+
if (options.pageSize) params.append("pageSize", options.pageSize.toString());
|
|
92
|
+
if (options.distance) params.append("distance", options.distance.toString());
|
|
93
|
+
params.append(
|
|
94
|
+
"calculateProvinceFromSearchString",
|
|
95
|
+
calculateProvinceFromSearchString ? "true" : "false"
|
|
96
|
+
);
|
|
97
|
+
params.append(
|
|
98
|
+
"calculateCityFromSearchString",
|
|
99
|
+
calculateCityFromSearchString ? "true" : "false"
|
|
100
|
+
);
|
|
78
101
|
if (options.partner) {
|
|
79
102
|
for (const [key, value] of Object.entries(options.partner)) {
|
|
80
103
|
params.append(`filter[${key}]`, value ? "true" : "false");
|
|
@@ -152,8 +175,7 @@ var formatServiceAddress = (address) => {
|
|
|
152
175
|
const newAddress = street1 + street2 + city + province + postalCode;
|
|
153
176
|
if (newAddress.endsWith(", ")) {
|
|
154
177
|
return newAddress.slice(0, -2);
|
|
155
|
-
} else
|
|
156
|
-
return newAddress;
|
|
178
|
+
} else return newAddress;
|
|
157
179
|
};
|
|
158
180
|
export {
|
|
159
181
|
CordsAPI,
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -12,17 +12,22 @@ export const ResourceOptions = {};
|
|
|
12
12
|
export const CordsAPI = ({
|
|
13
13
|
apiKey,
|
|
14
14
|
version = "production",
|
|
15
|
+
referer,
|
|
15
16
|
}: {
|
|
16
17
|
apiKey: string;
|
|
17
18
|
version?: "production" | "dev";
|
|
19
|
+
referer?: string;
|
|
18
20
|
}) => {
|
|
21
|
+
// Set the base URL for the Cords API
|
|
19
22
|
const baseUrl = version === "production" ? "https://api.cords.ai" : "https://api.cords.dev";
|
|
20
23
|
|
|
24
|
+
// Helper for making requests to the Cords API that applies the api key, referrer, and handles errors
|
|
21
25
|
const request = async (input: RequestInfo, init?: RequestInit) => {
|
|
22
26
|
const res = await fetch(input, {
|
|
23
27
|
...init,
|
|
24
28
|
headers: {
|
|
25
29
|
"x-api-key": apiKey,
|
|
30
|
+
...(referer ? { referer } : {}),
|
|
26
31
|
...init?.headers,
|
|
27
32
|
},
|
|
28
33
|
});
|
|
@@ -36,7 +41,15 @@ export const CordsAPI = ({
|
|
|
36
41
|
return res;
|
|
37
42
|
};
|
|
38
43
|
|
|
39
|
-
|
|
44
|
+
// Search for resources
|
|
45
|
+
const search = async (
|
|
46
|
+
q: string,
|
|
47
|
+
{
|
|
48
|
+
calculateCityFromSearchString = true,
|
|
49
|
+
calculateProvinceFromSearchString = true,
|
|
50
|
+
...options
|
|
51
|
+
}: SearchOptions
|
|
52
|
+
) => {
|
|
40
53
|
const url = new URL("/search", baseUrl);
|
|
41
54
|
const params = new URLSearchParams({
|
|
42
55
|
q,
|
|
@@ -50,6 +63,16 @@ export const CordsAPI = ({
|
|
|
50
63
|
if (options.pageSize) params.append("pageSize", options.pageSize.toString());
|
|
51
64
|
if (options.distance) params.append("distance", options.distance.toString());
|
|
52
65
|
|
|
66
|
+
// Add boolean parameters
|
|
67
|
+
params.append(
|
|
68
|
+
"calculateProvinceFromSearchString",
|
|
69
|
+
calculateProvinceFromSearchString ? "true" : "false"
|
|
70
|
+
);
|
|
71
|
+
params.append(
|
|
72
|
+
"calculateCityFromSearchString",
|
|
73
|
+
calculateCityFromSearchString ? "true" : "false"
|
|
74
|
+
);
|
|
75
|
+
|
|
53
76
|
// Add partner parameters
|
|
54
77
|
if (options.partner) {
|
|
55
78
|
for (const [key, value] of Object.entries(options.partner)) {
|
|
@@ -72,6 +95,7 @@ export const CordsAPI = ({
|
|
|
72
95
|
};
|
|
73
96
|
};
|
|
74
97
|
|
|
98
|
+
// Get related to a resource
|
|
75
99
|
const related = async (id: string) => {
|
|
76
100
|
const url = new URL(`/resource/${id}/related`, baseUrl);
|
|
77
101
|
|
|
@@ -84,6 +108,7 @@ export const CordsAPI = ({
|
|
|
84
108
|
return data as { data: ResourceType[] };
|
|
85
109
|
};
|
|
86
110
|
|
|
111
|
+
// Get a single resource by id
|
|
87
112
|
const resource = async (id: string) => {
|
|
88
113
|
const url = new URL(`/resource/${id}`, baseUrl);
|
|
89
114
|
|
|
@@ -96,6 +121,7 @@ export const CordsAPI = ({
|
|
|
96
121
|
return data as ResourceType;
|
|
97
122
|
};
|
|
98
123
|
|
|
124
|
+
// Get a list of resources by id
|
|
99
125
|
const resourceList = async (ids: string[]): Promise<{ data: ResourceType[] }> => {
|
|
100
126
|
if (ids.length === 0)
|
|
101
127
|
return {
|
|
@@ -111,6 +137,7 @@ export const CordsAPI = ({
|
|
|
111
137
|
return data as { data: ResourceType[] };
|
|
112
138
|
};
|
|
113
139
|
|
|
140
|
+
// Get the nearest neighbour to a resource
|
|
114
141
|
const nearestNeighbour = async (
|
|
115
142
|
id: string,
|
|
116
143
|
options: {
|
|
@@ -143,6 +170,7 @@ export const CordsAPI = ({
|
|
|
143
170
|
};
|
|
144
171
|
};
|
|
145
172
|
|
|
173
|
+
// Helper function to format a service address
|
|
146
174
|
export const formatServiceAddress = (address: ResourceAddressType) => {
|
|
147
175
|
const street1 = address.street1 ? address.street1 + ", " : "";
|
|
148
176
|
const street2 = address.street2 ? address.street2 + ", " : "";
|
package/src/types.ts
CHANGED
|
@@ -60,7 +60,12 @@ export type ResourceType = {
|
|
|
60
60
|
export type SearchResourceType = Omit<
|
|
61
61
|
ResourceType,
|
|
62
62
|
"website" | "email" | "phoneNumbers" | "addresses" | "address"
|
|
63
|
-
|
|
63
|
+
> & {
|
|
64
|
+
location: {
|
|
65
|
+
lat: number | null;
|
|
66
|
+
lng: number | null;
|
|
67
|
+
};
|
|
68
|
+
};
|
|
64
69
|
|
|
65
70
|
export type SearchOptions = {
|
|
66
71
|
lat: number;
|
|
@@ -73,6 +78,7 @@ export type SearchOptions = {
|
|
|
73
78
|
mentor?: boolean;
|
|
74
79
|
prosper?: boolean;
|
|
75
80
|
magnet?: boolean;
|
|
81
|
+
volunteer?: boolean;
|
|
76
82
|
};
|
|
77
83
|
delivery?: {
|
|
78
84
|
local?: boolean;
|
|
@@ -80,6 +86,8 @@ export type SearchOptions = {
|
|
|
80
86
|
provincial?: boolean;
|
|
81
87
|
national?: boolean;
|
|
82
88
|
};
|
|
89
|
+
calculateProvinceFromSearchString?: boolean;
|
|
90
|
+
calculateCityFromSearchString?: boolean;
|
|
83
91
|
};
|
|
84
92
|
|
|
85
93
|
export type CordsError = {
|