@cords/sdk 0.0.3 → 0.0.5
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 +11 -10
- package/dist/index.d.mts +112 -0
- package/dist/index.d.ts +112 -0
- package/dist/index.js +186 -0
- package/dist/index.mjs +162 -0
- package/package.json +1 -1
- package/src/index.ts +26 -14
- package/src/types.ts +12 -1
package/README.md
CHANGED
|
@@ -27,17 +27,18 @@ const cords = CordsAPI({ apiKey: "your_api_key_here" });
|
|
|
27
27
|
To perform a search, use the `search` method. You can pass a query string and optional search options:
|
|
28
28
|
|
|
29
29
|
```ts
|
|
30
|
-
const
|
|
31
|
-
page: 1,
|
|
30
|
+
const searchResults = await cords.search("query", {
|
|
32
31
|
lat: 40.7128,
|
|
33
32
|
lng: -74.006,
|
|
34
|
-
|
|
35
|
-
pageSize:
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
page: 1,
|
|
34
|
+
pageSize: 10,
|
|
35
|
+
distance: 100,
|
|
36
|
+
partner: {
|
|
37
|
+
"211": true,
|
|
38
38
|
mentor: true,
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
},
|
|
40
|
+
delivery: {
|
|
41
|
+
local: true,
|
|
41
42
|
},
|
|
42
43
|
});
|
|
43
44
|
```
|
|
@@ -73,5 +74,5 @@ The SDK also includes a utility function to format service addresses:
|
|
|
73
74
|
```ts
|
|
74
75
|
import { formatServiceAddress } from "@cords/sdk";
|
|
75
76
|
|
|
76
|
-
const formattedAddress = formatServiceAddress(resource.data.address)
|
|
77
|
-
```
|
|
77
|
+
const formattedAddress = formatServiceAddress(resource.data.address);
|
|
78
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
type LocalizedFieldType = {
|
|
2
|
+
en: string;
|
|
3
|
+
fr: string;
|
|
4
|
+
};
|
|
5
|
+
type ResourceAddressType = {
|
|
6
|
+
street1: string;
|
|
7
|
+
street2: string;
|
|
8
|
+
city: string;
|
|
9
|
+
postalCode: string;
|
|
10
|
+
province: string;
|
|
11
|
+
country: string;
|
|
12
|
+
lat: number | null;
|
|
13
|
+
lng: number | null;
|
|
14
|
+
};
|
|
15
|
+
type ResourceBodyType = {
|
|
16
|
+
fees: string;
|
|
17
|
+
hours: string;
|
|
18
|
+
topics: string;
|
|
19
|
+
twitter: string | null;
|
|
20
|
+
youtube: string | null;
|
|
21
|
+
facebook: string | null;
|
|
22
|
+
linkedin: string | null;
|
|
23
|
+
instagram: string | null;
|
|
24
|
+
languages: string;
|
|
25
|
+
eligibility: string;
|
|
26
|
+
recordOwner: string;
|
|
27
|
+
accessibility: string;
|
|
28
|
+
documentsRequired: string;
|
|
29
|
+
applicationProcess: string;
|
|
30
|
+
};
|
|
31
|
+
type ResourceType = {
|
|
32
|
+
id: string;
|
|
33
|
+
name: LocalizedFieldType;
|
|
34
|
+
description: LocalizedFieldType;
|
|
35
|
+
website: LocalizedFieldType;
|
|
36
|
+
email: LocalizedFieldType;
|
|
37
|
+
address: ResourceAddressType;
|
|
38
|
+
addresses: ResourceAddressType[];
|
|
39
|
+
phoneNumbers: {
|
|
40
|
+
phone: string;
|
|
41
|
+
name: string;
|
|
42
|
+
type: string;
|
|
43
|
+
}[];
|
|
44
|
+
partner: string;
|
|
45
|
+
delivery: "national" | "provincial" | "local" | "regional" | null;
|
|
46
|
+
body: {
|
|
47
|
+
en: ResourceBodyType | null;
|
|
48
|
+
fr: ResourceBodyType | null;
|
|
49
|
+
};
|
|
50
|
+
result: {
|
|
51
|
+
id: string;
|
|
52
|
+
distance: number | null;
|
|
53
|
+
vectorDistance: number;
|
|
54
|
+
} | null;
|
|
55
|
+
};
|
|
56
|
+
type SearchResourceType = Omit<ResourceType, "website" | "email" | "phoneNumbers" | "addresses" | "address">;
|
|
57
|
+
type SearchOptions = {
|
|
58
|
+
lat: number;
|
|
59
|
+
lng: number;
|
|
60
|
+
page?: number;
|
|
61
|
+
distance?: number;
|
|
62
|
+
pageSize?: number;
|
|
63
|
+
partner?: {
|
|
64
|
+
"211"?: boolean;
|
|
65
|
+
mentor?: boolean;
|
|
66
|
+
prosper?: boolean;
|
|
67
|
+
magnet?: boolean;
|
|
68
|
+
};
|
|
69
|
+
delivery?: {
|
|
70
|
+
local?: boolean;
|
|
71
|
+
regional?: boolean;
|
|
72
|
+
provincial?: boolean;
|
|
73
|
+
national?: boolean;
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
type CordsError = {
|
|
77
|
+
detail: string;
|
|
78
|
+
status: number;
|
|
79
|
+
title: string;
|
|
80
|
+
type: string;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
declare const ResourceOptions: {};
|
|
84
|
+
declare const CordsAPI: ({ apiKey, version, }: {
|
|
85
|
+
apiKey: string;
|
|
86
|
+
version?: "production" | "dev";
|
|
87
|
+
}) => {
|
|
88
|
+
search: (q: string, options: SearchOptions) => Promise<{
|
|
89
|
+
data: SearchResourceType[];
|
|
90
|
+
meta: {
|
|
91
|
+
total: number;
|
|
92
|
+
lat: number;
|
|
93
|
+
lng: number;
|
|
94
|
+
};
|
|
95
|
+
}>;
|
|
96
|
+
related: (id: string) => Promise<{
|
|
97
|
+
data: ResourceType[];
|
|
98
|
+
}>;
|
|
99
|
+
resource: (id: string) => Promise<ResourceType>;
|
|
100
|
+
resourceList: (ids: string[]) => Promise<{
|
|
101
|
+
data: ResourceType[];
|
|
102
|
+
}>;
|
|
103
|
+
nearestNeighbour: (id: string, options: {
|
|
104
|
+
lat: number;
|
|
105
|
+
lng: number;
|
|
106
|
+
}) => Promise<{
|
|
107
|
+
data: ResourceType[];
|
|
108
|
+
}>;
|
|
109
|
+
};
|
|
110
|
+
declare const formatServiceAddress: (address: ResourceAddressType) => string;
|
|
111
|
+
|
|
112
|
+
export { CordsAPI, type CordsError, type ResourceAddressType, type ResourceBodyType, ResourceOptions, type ResourceType, type SearchOptions, type SearchResourceType, formatServiceAddress };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
type LocalizedFieldType = {
|
|
2
|
+
en: string;
|
|
3
|
+
fr: string;
|
|
4
|
+
};
|
|
5
|
+
type ResourceAddressType = {
|
|
6
|
+
street1: string;
|
|
7
|
+
street2: string;
|
|
8
|
+
city: string;
|
|
9
|
+
postalCode: string;
|
|
10
|
+
province: string;
|
|
11
|
+
country: string;
|
|
12
|
+
lat: number | null;
|
|
13
|
+
lng: number | null;
|
|
14
|
+
};
|
|
15
|
+
type ResourceBodyType = {
|
|
16
|
+
fees: string;
|
|
17
|
+
hours: string;
|
|
18
|
+
topics: string;
|
|
19
|
+
twitter: string | null;
|
|
20
|
+
youtube: string | null;
|
|
21
|
+
facebook: string | null;
|
|
22
|
+
linkedin: string | null;
|
|
23
|
+
instagram: string | null;
|
|
24
|
+
languages: string;
|
|
25
|
+
eligibility: string;
|
|
26
|
+
recordOwner: string;
|
|
27
|
+
accessibility: string;
|
|
28
|
+
documentsRequired: string;
|
|
29
|
+
applicationProcess: string;
|
|
30
|
+
};
|
|
31
|
+
type ResourceType = {
|
|
32
|
+
id: string;
|
|
33
|
+
name: LocalizedFieldType;
|
|
34
|
+
description: LocalizedFieldType;
|
|
35
|
+
website: LocalizedFieldType;
|
|
36
|
+
email: LocalizedFieldType;
|
|
37
|
+
address: ResourceAddressType;
|
|
38
|
+
addresses: ResourceAddressType[];
|
|
39
|
+
phoneNumbers: {
|
|
40
|
+
phone: string;
|
|
41
|
+
name: string;
|
|
42
|
+
type: string;
|
|
43
|
+
}[];
|
|
44
|
+
partner: string;
|
|
45
|
+
delivery: "national" | "provincial" | "local" | "regional" | null;
|
|
46
|
+
body: {
|
|
47
|
+
en: ResourceBodyType | null;
|
|
48
|
+
fr: ResourceBodyType | null;
|
|
49
|
+
};
|
|
50
|
+
result: {
|
|
51
|
+
id: string;
|
|
52
|
+
distance: number | null;
|
|
53
|
+
vectorDistance: number;
|
|
54
|
+
} | null;
|
|
55
|
+
};
|
|
56
|
+
type SearchResourceType = Omit<ResourceType, "website" | "email" | "phoneNumbers" | "addresses" | "address">;
|
|
57
|
+
type SearchOptions = {
|
|
58
|
+
lat: number;
|
|
59
|
+
lng: number;
|
|
60
|
+
page?: number;
|
|
61
|
+
distance?: number;
|
|
62
|
+
pageSize?: number;
|
|
63
|
+
partner?: {
|
|
64
|
+
"211"?: boolean;
|
|
65
|
+
mentor?: boolean;
|
|
66
|
+
prosper?: boolean;
|
|
67
|
+
magnet?: boolean;
|
|
68
|
+
};
|
|
69
|
+
delivery?: {
|
|
70
|
+
local?: boolean;
|
|
71
|
+
regional?: boolean;
|
|
72
|
+
provincial?: boolean;
|
|
73
|
+
national?: boolean;
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
type CordsError = {
|
|
77
|
+
detail: string;
|
|
78
|
+
status: number;
|
|
79
|
+
title: string;
|
|
80
|
+
type: string;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
declare const ResourceOptions: {};
|
|
84
|
+
declare const CordsAPI: ({ apiKey, version, }: {
|
|
85
|
+
apiKey: string;
|
|
86
|
+
version?: "production" | "dev";
|
|
87
|
+
}) => {
|
|
88
|
+
search: (q: string, options: SearchOptions) => Promise<{
|
|
89
|
+
data: SearchResourceType[];
|
|
90
|
+
meta: {
|
|
91
|
+
total: number;
|
|
92
|
+
lat: number;
|
|
93
|
+
lng: number;
|
|
94
|
+
};
|
|
95
|
+
}>;
|
|
96
|
+
related: (id: string) => Promise<{
|
|
97
|
+
data: ResourceType[];
|
|
98
|
+
}>;
|
|
99
|
+
resource: (id: string) => Promise<ResourceType>;
|
|
100
|
+
resourceList: (ids: string[]) => Promise<{
|
|
101
|
+
data: ResourceType[];
|
|
102
|
+
}>;
|
|
103
|
+
nearestNeighbour: (id: string, options: {
|
|
104
|
+
lat: number;
|
|
105
|
+
lng: number;
|
|
106
|
+
}) => Promise<{
|
|
107
|
+
data: ResourceType[];
|
|
108
|
+
}>;
|
|
109
|
+
};
|
|
110
|
+
declare const formatServiceAddress: (address: ResourceAddressType) => string;
|
|
111
|
+
|
|
112
|
+
export { CordsAPI, type CordsError, type ResourceAddressType, type ResourceBodyType, ResourceOptions, type ResourceType, type SearchOptions, type SearchResourceType, formatServiceAddress };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
23
|
+
var __export = (target, all) => {
|
|
24
|
+
for (var name in all)
|
|
25
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
26
|
+
};
|
|
27
|
+
var __copyProps = (to, from, except, desc) => {
|
|
28
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
29
|
+
for (let key of __getOwnPropNames(from))
|
|
30
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
31
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
32
|
+
}
|
|
33
|
+
return to;
|
|
34
|
+
};
|
|
35
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
36
|
+
var __async = (__this, __arguments, generator) => {
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
var fulfilled = (value) => {
|
|
39
|
+
try {
|
|
40
|
+
step(generator.next(value));
|
|
41
|
+
} catch (e) {
|
|
42
|
+
reject(e);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var rejected = (value) => {
|
|
46
|
+
try {
|
|
47
|
+
step(generator.throw(value));
|
|
48
|
+
} catch (e) {
|
|
49
|
+
reject(e);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
53
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/index.ts
|
|
58
|
+
var src_exports = {};
|
|
59
|
+
__export(src_exports, {
|
|
60
|
+
CordsAPI: () => CordsAPI,
|
|
61
|
+
ResourceOptions: () => ResourceOptions,
|
|
62
|
+
formatServiceAddress: () => formatServiceAddress
|
|
63
|
+
});
|
|
64
|
+
module.exports = __toCommonJS(src_exports);
|
|
65
|
+
var ResourceOptions = {};
|
|
66
|
+
var CordsAPI = ({
|
|
67
|
+
apiKey,
|
|
68
|
+
version = "production"
|
|
69
|
+
}) => {
|
|
70
|
+
const baseUrl = version === "production" ? "https://api.cords.ai" : "https://api.cords.dev";
|
|
71
|
+
const request = (input, init) => __async(void 0, null, function* () {
|
|
72
|
+
const res = yield fetch(input, __spreadProps(__spreadValues({}, init), {
|
|
73
|
+
headers: __spreadValues({
|
|
74
|
+
"x-api-key": apiKey
|
|
75
|
+
}, init == null ? void 0 : init.headers)
|
|
76
|
+
}));
|
|
77
|
+
if (!res.ok) {
|
|
78
|
+
if (res.status === 403)
|
|
79
|
+
throw new Error("Bad API key. Ensure you have a valid API key.");
|
|
80
|
+
const data = yield res.json();
|
|
81
|
+
if (data.detail)
|
|
82
|
+
throw new Error(data.detail);
|
|
83
|
+
else
|
|
84
|
+
throw new Error("An error occurred");
|
|
85
|
+
}
|
|
86
|
+
return res;
|
|
87
|
+
});
|
|
88
|
+
const search = (q, options) => __async(void 0, null, function* () {
|
|
89
|
+
const url = new URL("/search", baseUrl);
|
|
90
|
+
const params = new URLSearchParams({
|
|
91
|
+
q
|
|
92
|
+
});
|
|
93
|
+
params.append("lat", options.lat.toString());
|
|
94
|
+
params.append("lng", options.lng.toString());
|
|
95
|
+
if (options.page)
|
|
96
|
+
params.append("page", options.page.toString());
|
|
97
|
+
if (options.pageSize)
|
|
98
|
+
params.append("pageSize", options.pageSize.toString());
|
|
99
|
+
if (options.distance)
|
|
100
|
+
params.append("distance", options.distance.toString());
|
|
101
|
+
if (options.partner) {
|
|
102
|
+
for (const [key, value] of Object.entries(options.partner)) {
|
|
103
|
+
params.append(`filter[${key}]`, value ? "true" : "false");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (options.delivery) {
|
|
107
|
+
for (const [key, value] of Object.entries(options.delivery)) {
|
|
108
|
+
params.append(`filter[delivery][${key}]`, value ? "true" : "false");
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const res = yield request(`${url.toString()}?${params}`);
|
|
112
|
+
const data = yield res.json();
|
|
113
|
+
return data;
|
|
114
|
+
});
|
|
115
|
+
const related = (id) => __async(void 0, null, function* () {
|
|
116
|
+
const url = new URL(`/resource/${id}/related`, baseUrl);
|
|
117
|
+
const res = yield request(url.toString());
|
|
118
|
+
if (!res.ok) {
|
|
119
|
+
const data2 = yield res.json();
|
|
120
|
+
throw new Error(data2.detail);
|
|
121
|
+
}
|
|
122
|
+
const data = yield res.json();
|
|
123
|
+
return data;
|
|
124
|
+
});
|
|
125
|
+
const resource = (id) => __async(void 0, null, function* () {
|
|
126
|
+
const url = new URL(`/resource/${id}`, baseUrl);
|
|
127
|
+
const res = yield request(url.toString());
|
|
128
|
+
if (!res.ok) {
|
|
129
|
+
const data2 = yield res.json();
|
|
130
|
+
throw new Error(data2.detail);
|
|
131
|
+
}
|
|
132
|
+
const data = yield res.json();
|
|
133
|
+
return data;
|
|
134
|
+
});
|
|
135
|
+
const resourceList = (ids) => __async(void 0, null, function* () {
|
|
136
|
+
if (ids.length === 0)
|
|
137
|
+
return {
|
|
138
|
+
data: []
|
|
139
|
+
};
|
|
140
|
+
const params = new URLSearchParams();
|
|
141
|
+
ids.forEach((id, index) => params.append(`ids[${index}]`, id));
|
|
142
|
+
const url = new URL(`/search?${params.toString()}`, baseUrl);
|
|
143
|
+
const res = yield request(url.toString());
|
|
144
|
+
const data = yield res.json();
|
|
145
|
+
return data;
|
|
146
|
+
});
|
|
147
|
+
const nearestNeighbour = (id, options) => __async(void 0, null, function* () {
|
|
148
|
+
const url = new URL(`/resource/${id}/nearest-neighbor`, baseUrl);
|
|
149
|
+
const params = new URLSearchParams({
|
|
150
|
+
lat: options.lat.toString(),
|
|
151
|
+
lng: options.lng.toString()
|
|
152
|
+
});
|
|
153
|
+
const res = yield request(url.toString() + "?delivery=local&" + params.toString());
|
|
154
|
+
if (!res.ok) {
|
|
155
|
+
const data2 = yield res.json();
|
|
156
|
+
throw new Error(data2.detail);
|
|
157
|
+
}
|
|
158
|
+
const data = yield res.json();
|
|
159
|
+
return data;
|
|
160
|
+
});
|
|
161
|
+
return {
|
|
162
|
+
search,
|
|
163
|
+
related,
|
|
164
|
+
resource,
|
|
165
|
+
resourceList,
|
|
166
|
+
nearestNeighbour
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
var formatServiceAddress = (address) => {
|
|
170
|
+
const street1 = address.street1 ? address.street1 + ", " : "";
|
|
171
|
+
const street2 = address.street2 ? address.street2 + ", " : "";
|
|
172
|
+
const city = address.city ? address.city + ", " : "";
|
|
173
|
+
const province = address.province ? address.province + ", " : "";
|
|
174
|
+
const postalCode = address.postalCode ? address.postalCode : "";
|
|
175
|
+
const newAddress = street1 + street2 + city + province + postalCode;
|
|
176
|
+
if (newAddress.endsWith(", ")) {
|
|
177
|
+
return newAddress.slice(0, -2);
|
|
178
|
+
} else
|
|
179
|
+
return newAddress;
|
|
180
|
+
};
|
|
181
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
182
|
+
0 && (module.exports = {
|
|
183
|
+
CordsAPI,
|
|
184
|
+
ResourceOptions,
|
|
185
|
+
formatServiceAddress
|
|
186
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/index.ts
|
|
42
|
+
var ResourceOptions = {};
|
|
43
|
+
var CordsAPI = ({
|
|
44
|
+
apiKey,
|
|
45
|
+
version = "production"
|
|
46
|
+
}) => {
|
|
47
|
+
const baseUrl = version === "production" ? "https://api.cords.ai" : "https://api.cords.dev";
|
|
48
|
+
const request = (input, init) => __async(void 0, null, function* () {
|
|
49
|
+
const res = yield fetch(input, __spreadProps(__spreadValues({}, init), {
|
|
50
|
+
headers: __spreadValues({
|
|
51
|
+
"x-api-key": apiKey
|
|
52
|
+
}, init == null ? void 0 : init.headers)
|
|
53
|
+
}));
|
|
54
|
+
if (!res.ok) {
|
|
55
|
+
if (res.status === 403)
|
|
56
|
+
throw new Error("Bad API key. Ensure you have a valid API key.");
|
|
57
|
+
const data = yield res.json();
|
|
58
|
+
if (data.detail)
|
|
59
|
+
throw new Error(data.detail);
|
|
60
|
+
else
|
|
61
|
+
throw new Error("An error occurred");
|
|
62
|
+
}
|
|
63
|
+
return res;
|
|
64
|
+
});
|
|
65
|
+
const search = (q, options) => __async(void 0, null, function* () {
|
|
66
|
+
const url = new URL("/search", baseUrl);
|
|
67
|
+
const params = new URLSearchParams({
|
|
68
|
+
q
|
|
69
|
+
});
|
|
70
|
+
params.append("lat", options.lat.toString());
|
|
71
|
+
params.append("lng", options.lng.toString());
|
|
72
|
+
if (options.page)
|
|
73
|
+
params.append("page", options.page.toString());
|
|
74
|
+
if (options.pageSize)
|
|
75
|
+
params.append("pageSize", options.pageSize.toString());
|
|
76
|
+
if (options.distance)
|
|
77
|
+
params.append("distance", options.distance.toString());
|
|
78
|
+
if (options.partner) {
|
|
79
|
+
for (const [key, value] of Object.entries(options.partner)) {
|
|
80
|
+
params.append(`filter[${key}]`, value ? "true" : "false");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (options.delivery) {
|
|
84
|
+
for (const [key, value] of Object.entries(options.delivery)) {
|
|
85
|
+
params.append(`filter[delivery][${key}]`, value ? "true" : "false");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const res = yield request(`${url.toString()}?${params}`);
|
|
89
|
+
const data = yield res.json();
|
|
90
|
+
return data;
|
|
91
|
+
});
|
|
92
|
+
const related = (id) => __async(void 0, null, function* () {
|
|
93
|
+
const url = new URL(`/resource/${id}/related`, baseUrl);
|
|
94
|
+
const res = yield request(url.toString());
|
|
95
|
+
if (!res.ok) {
|
|
96
|
+
const data2 = yield res.json();
|
|
97
|
+
throw new Error(data2.detail);
|
|
98
|
+
}
|
|
99
|
+
const data = yield res.json();
|
|
100
|
+
return data;
|
|
101
|
+
});
|
|
102
|
+
const resource = (id) => __async(void 0, null, function* () {
|
|
103
|
+
const url = new URL(`/resource/${id}`, baseUrl);
|
|
104
|
+
const res = yield request(url.toString());
|
|
105
|
+
if (!res.ok) {
|
|
106
|
+
const data2 = yield res.json();
|
|
107
|
+
throw new Error(data2.detail);
|
|
108
|
+
}
|
|
109
|
+
const data = yield res.json();
|
|
110
|
+
return data;
|
|
111
|
+
});
|
|
112
|
+
const resourceList = (ids) => __async(void 0, null, function* () {
|
|
113
|
+
if (ids.length === 0)
|
|
114
|
+
return {
|
|
115
|
+
data: []
|
|
116
|
+
};
|
|
117
|
+
const params = new URLSearchParams();
|
|
118
|
+
ids.forEach((id, index) => params.append(`ids[${index}]`, id));
|
|
119
|
+
const url = new URL(`/search?${params.toString()}`, baseUrl);
|
|
120
|
+
const res = yield request(url.toString());
|
|
121
|
+
const data = yield res.json();
|
|
122
|
+
return data;
|
|
123
|
+
});
|
|
124
|
+
const nearestNeighbour = (id, options) => __async(void 0, null, function* () {
|
|
125
|
+
const url = new URL(`/resource/${id}/nearest-neighbor`, baseUrl);
|
|
126
|
+
const params = new URLSearchParams({
|
|
127
|
+
lat: options.lat.toString(),
|
|
128
|
+
lng: options.lng.toString()
|
|
129
|
+
});
|
|
130
|
+
const res = yield request(url.toString() + "?delivery=local&" + params.toString());
|
|
131
|
+
if (!res.ok) {
|
|
132
|
+
const data2 = yield res.json();
|
|
133
|
+
throw new Error(data2.detail);
|
|
134
|
+
}
|
|
135
|
+
const data = yield res.json();
|
|
136
|
+
return data;
|
|
137
|
+
});
|
|
138
|
+
return {
|
|
139
|
+
search,
|
|
140
|
+
related,
|
|
141
|
+
resource,
|
|
142
|
+
resourceList,
|
|
143
|
+
nearestNeighbour
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
var formatServiceAddress = (address) => {
|
|
147
|
+
const street1 = address.street1 ? address.street1 + ", " : "";
|
|
148
|
+
const street2 = address.street2 ? address.street2 + ", " : "";
|
|
149
|
+
const city = address.city ? address.city + ", " : "";
|
|
150
|
+
const province = address.province ? address.province + ", " : "";
|
|
151
|
+
const postalCode = address.postalCode ? address.postalCode : "";
|
|
152
|
+
const newAddress = street1 + street2 + city + province + postalCode;
|
|
153
|
+
if (newAddress.endsWith(", ")) {
|
|
154
|
+
return newAddress.slice(0, -2);
|
|
155
|
+
} else
|
|
156
|
+
return newAddress;
|
|
157
|
+
};
|
|
158
|
+
export {
|
|
159
|
+
CordsAPI,
|
|
160
|
+
ResourceOptions,
|
|
161
|
+
formatServiceAddress
|
|
162
|
+
};
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import type {
|
|
2
|
+
CordsError,
|
|
3
|
+
ResourceAddressType,
|
|
4
|
+
ResourceType,
|
|
5
|
+
SearchOptions,
|
|
6
|
+
SearchResourceType,
|
|
7
|
+
} from "./types";
|
|
3
8
|
export * from "./types";
|
|
4
9
|
|
|
5
10
|
export const ResourceOptions = {};
|
|
6
11
|
|
|
7
|
-
const baseUrl = "https://api.cords.ai";
|
|
8
|
-
|
|
9
12
|
export const CordsAPI = ({
|
|
10
13
|
apiKey,
|
|
11
14
|
version = "production",
|
|
@@ -33,22 +36,31 @@ export const CordsAPI = ({
|
|
|
33
36
|
return res;
|
|
34
37
|
};
|
|
35
38
|
|
|
36
|
-
const search = async (q: string, options
|
|
39
|
+
const search = async (q: string, options: SearchOptions) => {
|
|
37
40
|
const url = new URL("/search", baseUrl);
|
|
38
41
|
const params = new URLSearchParams({
|
|
39
42
|
q,
|
|
40
43
|
});
|
|
41
44
|
|
|
45
|
+
params.append("lat", options.lat.toString());
|
|
46
|
+
params.append("lng", options.lng.toString());
|
|
47
|
+
|
|
42
48
|
// Add top-level parameters
|
|
43
|
-
if (options
|
|
44
|
-
if (options
|
|
45
|
-
if (options
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
if (options.page) params.append("page", options.page.toString());
|
|
50
|
+
if (options.pageSize) params.append("pageSize", options.pageSize.toString());
|
|
51
|
+
if (options.distance) params.append("distance", options.distance.toString());
|
|
52
|
+
|
|
53
|
+
// Add partner parameters
|
|
54
|
+
if (options.partner) {
|
|
55
|
+
for (const [key, value] of Object.entries(options.partner)) {
|
|
56
|
+
params.append(`filter[${key}]`, value ? "true" : "false");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Add delivery parameters
|
|
61
|
+
if (options.delivery) {
|
|
62
|
+
for (const [key, value] of Object.entries(options.delivery)) {
|
|
63
|
+
params.append(`filter[delivery][${key}]`, value ? "true" : "false");
|
|
52
64
|
}
|
|
53
65
|
}
|
|
54
66
|
|
package/src/types.ts
CHANGED
|
@@ -50,6 +50,11 @@ export type ResourceType = {
|
|
|
50
50
|
en: ResourceBodyType | null;
|
|
51
51
|
fr: ResourceBodyType | null;
|
|
52
52
|
};
|
|
53
|
+
result: {
|
|
54
|
+
id: string;
|
|
55
|
+
distance: number | null;
|
|
56
|
+
vectorDistance: number;
|
|
57
|
+
} | null;
|
|
53
58
|
};
|
|
54
59
|
|
|
55
60
|
export type SearchResourceType = Omit<
|
|
@@ -63,12 +68,18 @@ export type SearchOptions = {
|
|
|
63
68
|
page?: number;
|
|
64
69
|
distance?: number;
|
|
65
70
|
pageSize?: number;
|
|
66
|
-
|
|
71
|
+
partner?: {
|
|
67
72
|
"211"?: boolean;
|
|
68
73
|
mentor?: boolean;
|
|
69
74
|
prosper?: boolean;
|
|
70
75
|
magnet?: boolean;
|
|
71
76
|
};
|
|
77
|
+
delivery?: {
|
|
78
|
+
local?: boolean;
|
|
79
|
+
regional?: boolean;
|
|
80
|
+
provincial?: boolean;
|
|
81
|
+
national?: boolean;
|
|
82
|
+
};
|
|
72
83
|
};
|
|
73
84
|
|
|
74
85
|
export type CordsError = {
|