@cords/sdk 0.0.1
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 +77 -0
- package/dist/index.d.mts +79 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.js +168 -0
- package/dist/index.mjs +144 -0
- package/package.json +24 -0
- package/src/index.ts +113 -0
- package/src/types.ts +53 -0
- package/tsconfig.json +12 -0
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Cords API SDK
|
|
2
|
+
|
|
3
|
+
The Cords API SDK provides a convenient way to interact with the Cords API, offering a set of methods to perform various operations such as searching, fetching related resources, and retrieving individual resources. This SDK is designed to simplify the process of integrating Cords API functionalities into your applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
You can install the SDK using npm or any other package manager:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @cords/sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Initialization
|
|
16
|
+
|
|
17
|
+
First, import the `CordsAPI` function and initialize it with your API key:
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
import { CordsAPI } from "@cords/sdk";
|
|
21
|
+
|
|
22
|
+
const cords = CordsAPI({ apiKey: "your_api_key_here" });
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Searching
|
|
26
|
+
|
|
27
|
+
To perform a search, use the `search` method. You can pass a query string and optional search options:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
const results = await cords.search("query", {
|
|
31
|
+
page: 1,
|
|
32
|
+
lat: 40.7128,
|
|
33
|
+
lng: -74.006,
|
|
34
|
+
distance: 10,
|
|
35
|
+
pageSize: 20,
|
|
36
|
+
filter: {
|
|
37
|
+
211: true,
|
|
38
|
+
mentor: true,
|
|
39
|
+
prosper: true,
|
|
40
|
+
magnet: true,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Fetching Related Resources
|
|
46
|
+
|
|
47
|
+
To fetch resources related to a specific resource, use the `related` method:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
const relatedResources = await cords.related("resource_id");
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Retrieving a Single Resource
|
|
54
|
+
|
|
55
|
+
To retrieve a single resource by its ID, use the `resource` method:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
const resource = await cords.resource("resource_id");
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Fetching Multiple Resources
|
|
62
|
+
|
|
63
|
+
To fetch multiple resources by their IDs, use the `resourceList` method:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const resources = await cords.resourceList(["resource_id_1", "resource_id_2"]);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Formatting Service Address
|
|
70
|
+
|
|
71
|
+
The SDK also includes a utility function to format service addresses:
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { formatServiceAddress } from "@cords/sdk";
|
|
75
|
+
|
|
76
|
+
const formattedAddress = formatServiceAddress(resource.data.address)
|
|
77
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
type ResourceAddressType$1 = {
|
|
2
|
+
street1: string;
|
|
3
|
+
street2: string;
|
|
4
|
+
city: string;
|
|
5
|
+
postalCode: string;
|
|
6
|
+
province: string;
|
|
7
|
+
country: string;
|
|
8
|
+
lat: number | null;
|
|
9
|
+
lng: number | null;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type LocalizedFieldType = {
|
|
13
|
+
en: string;
|
|
14
|
+
fr: string;
|
|
15
|
+
};
|
|
16
|
+
type ResourceAddressType = {
|
|
17
|
+
street1: string;
|
|
18
|
+
street2: string;
|
|
19
|
+
city: string;
|
|
20
|
+
postalCode: string;
|
|
21
|
+
province: string;
|
|
22
|
+
country: string;
|
|
23
|
+
lat: number | null;
|
|
24
|
+
lng: number | null;
|
|
25
|
+
};
|
|
26
|
+
type ResourceType = {
|
|
27
|
+
id: string;
|
|
28
|
+
name: LocalizedFieldType;
|
|
29
|
+
description: LocalizedFieldType;
|
|
30
|
+
website: LocalizedFieldType;
|
|
31
|
+
email: LocalizedFieldType;
|
|
32
|
+
address: ResourceAddressType;
|
|
33
|
+
addresses: ResourceAddressType[];
|
|
34
|
+
phoneNumbers: {
|
|
35
|
+
phone: string;
|
|
36
|
+
name: string;
|
|
37
|
+
type: string;
|
|
38
|
+
}[];
|
|
39
|
+
partner: string;
|
|
40
|
+
delivery: "national" | "provincial" | "local" | "regional" | null;
|
|
41
|
+
};
|
|
42
|
+
type SearchOptions = {
|
|
43
|
+
page?: number;
|
|
44
|
+
lat?: number;
|
|
45
|
+
lng?: number;
|
|
46
|
+
distance?: number;
|
|
47
|
+
pageSize?: number;
|
|
48
|
+
filter?: {
|
|
49
|
+
"211"?: boolean;
|
|
50
|
+
mentor?: boolean;
|
|
51
|
+
prosper?: boolean;
|
|
52
|
+
magnet?: boolean;
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
type CordsError = {
|
|
56
|
+
detail: string;
|
|
57
|
+
status: number;
|
|
58
|
+
title: string;
|
|
59
|
+
type: string;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
declare const ResourceOptions: {};
|
|
63
|
+
declare const CordsAPI: ({ apiKey }: {
|
|
64
|
+
apiKey: string;
|
|
65
|
+
}) => {
|
|
66
|
+
search: (q: string, options?: SearchOptions) => Promise<{
|
|
67
|
+
data: ResourceType[];
|
|
68
|
+
}>;
|
|
69
|
+
related: (id: string) => Promise<{
|
|
70
|
+
data: ResourceType[];
|
|
71
|
+
}>;
|
|
72
|
+
resource: (id: string) => Promise<ResourceType>;
|
|
73
|
+
resourceList: (ids: string[]) => Promise<{
|
|
74
|
+
data: ResourceType[];
|
|
75
|
+
}>;
|
|
76
|
+
};
|
|
77
|
+
declare const formatServiceAddress: (address: ResourceAddressType$1) => string;
|
|
78
|
+
|
|
79
|
+
export { CordsAPI, type CordsError, type ResourceAddressType, ResourceOptions, type ResourceType, type SearchOptions, formatServiceAddress };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
type ResourceAddressType$1 = {
|
|
2
|
+
street1: string;
|
|
3
|
+
street2: string;
|
|
4
|
+
city: string;
|
|
5
|
+
postalCode: string;
|
|
6
|
+
province: string;
|
|
7
|
+
country: string;
|
|
8
|
+
lat: number | null;
|
|
9
|
+
lng: number | null;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type LocalizedFieldType = {
|
|
13
|
+
en: string;
|
|
14
|
+
fr: string;
|
|
15
|
+
};
|
|
16
|
+
type ResourceAddressType = {
|
|
17
|
+
street1: string;
|
|
18
|
+
street2: string;
|
|
19
|
+
city: string;
|
|
20
|
+
postalCode: string;
|
|
21
|
+
province: string;
|
|
22
|
+
country: string;
|
|
23
|
+
lat: number | null;
|
|
24
|
+
lng: number | null;
|
|
25
|
+
};
|
|
26
|
+
type ResourceType = {
|
|
27
|
+
id: string;
|
|
28
|
+
name: LocalizedFieldType;
|
|
29
|
+
description: LocalizedFieldType;
|
|
30
|
+
website: LocalizedFieldType;
|
|
31
|
+
email: LocalizedFieldType;
|
|
32
|
+
address: ResourceAddressType;
|
|
33
|
+
addresses: ResourceAddressType[];
|
|
34
|
+
phoneNumbers: {
|
|
35
|
+
phone: string;
|
|
36
|
+
name: string;
|
|
37
|
+
type: string;
|
|
38
|
+
}[];
|
|
39
|
+
partner: string;
|
|
40
|
+
delivery: "national" | "provincial" | "local" | "regional" | null;
|
|
41
|
+
};
|
|
42
|
+
type SearchOptions = {
|
|
43
|
+
page?: number;
|
|
44
|
+
lat?: number;
|
|
45
|
+
lng?: number;
|
|
46
|
+
distance?: number;
|
|
47
|
+
pageSize?: number;
|
|
48
|
+
filter?: {
|
|
49
|
+
"211"?: boolean;
|
|
50
|
+
mentor?: boolean;
|
|
51
|
+
prosper?: boolean;
|
|
52
|
+
magnet?: boolean;
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
type CordsError = {
|
|
56
|
+
detail: string;
|
|
57
|
+
status: number;
|
|
58
|
+
title: string;
|
|
59
|
+
type: string;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
declare const ResourceOptions: {};
|
|
63
|
+
declare const CordsAPI: ({ apiKey }: {
|
|
64
|
+
apiKey: string;
|
|
65
|
+
}) => {
|
|
66
|
+
search: (q: string, options?: SearchOptions) => Promise<{
|
|
67
|
+
data: ResourceType[];
|
|
68
|
+
}>;
|
|
69
|
+
related: (id: string) => Promise<{
|
|
70
|
+
data: ResourceType[];
|
|
71
|
+
}>;
|
|
72
|
+
resource: (id: string) => Promise<ResourceType>;
|
|
73
|
+
resourceList: (ids: string[]) => Promise<{
|
|
74
|
+
data: ResourceType[];
|
|
75
|
+
}>;
|
|
76
|
+
};
|
|
77
|
+
declare const formatServiceAddress: (address: ResourceAddressType$1) => string;
|
|
78
|
+
|
|
79
|
+
export { CordsAPI, type CordsError, type ResourceAddressType, ResourceOptions, type ResourceType, type SearchOptions, formatServiceAddress };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
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 baseUrl = "https://api.cords.ai";
|
|
67
|
+
var CordsAPI = ({ apiKey }) => {
|
|
68
|
+
const request = (input, init) => __async(void 0, null, function* () {
|
|
69
|
+
const res = yield fetch(input, __spreadProps(__spreadValues({}, init), {
|
|
70
|
+
headers: __spreadValues({
|
|
71
|
+
"x-api-key": apiKey
|
|
72
|
+
}, init == null ? void 0 : init.headers)
|
|
73
|
+
}));
|
|
74
|
+
if (!res.ok) {
|
|
75
|
+
if (res.status === 403)
|
|
76
|
+
throw new Error("Bad API key. Ensure you have a valid API key.");
|
|
77
|
+
const data = yield res.json();
|
|
78
|
+
if (data.detail)
|
|
79
|
+
throw new Error(data.detail);
|
|
80
|
+
else
|
|
81
|
+
throw new Error("An error occurred");
|
|
82
|
+
}
|
|
83
|
+
return res;
|
|
84
|
+
});
|
|
85
|
+
const search = (q, options) => __async(void 0, null, function* () {
|
|
86
|
+
const url = new URL("/search", baseUrl);
|
|
87
|
+
const params = new URLSearchParams({
|
|
88
|
+
q
|
|
89
|
+
});
|
|
90
|
+
if ((options == null ? void 0 : options.page) !== void 0)
|
|
91
|
+
params.append("page", options.page.toString());
|
|
92
|
+
if ((options == null ? void 0 : options.lat) !== void 0)
|
|
93
|
+
params.append("lat", options.lat.toString());
|
|
94
|
+
if ((options == null ? void 0 : options.lng) !== void 0)
|
|
95
|
+
params.append("lng", options.lng.toString());
|
|
96
|
+
if ((options == null ? void 0 : options.distance) !== void 0)
|
|
97
|
+
params.append("distance", options.distance.toString());
|
|
98
|
+
if ((options == null ? void 0 : options.filter) !== void 0) {
|
|
99
|
+
for (const [key, value] of Object.entries(options.filter)) {
|
|
100
|
+
if (value)
|
|
101
|
+
params.append(`filter[${key}]`, "true");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const res = yield request(`${url}?${params}`);
|
|
105
|
+
const data = yield res.json();
|
|
106
|
+
return data;
|
|
107
|
+
});
|
|
108
|
+
const related = (id) => __async(void 0, null, function* () {
|
|
109
|
+
const url = new URL(`/resource/${id}/related`, baseUrl);
|
|
110
|
+
const res = yield request(`${url}`);
|
|
111
|
+
if (!res.ok) {
|
|
112
|
+
const data2 = yield res.json();
|
|
113
|
+
throw new Error(data2.detail);
|
|
114
|
+
}
|
|
115
|
+
const data = yield res.json();
|
|
116
|
+
return data;
|
|
117
|
+
});
|
|
118
|
+
const resource = (id) => __async(void 0, null, function* () {
|
|
119
|
+
const url = new URL(`/resource/${id}`, baseUrl);
|
|
120
|
+
const res = yield fetch(url, {
|
|
121
|
+
headers: {
|
|
122
|
+
"x-api-key": apiKey
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
if (!res.ok) {
|
|
126
|
+
const data2 = yield res.json();
|
|
127
|
+
throw new Error(data2.detail);
|
|
128
|
+
}
|
|
129
|
+
const data = yield res.json();
|
|
130
|
+
return data;
|
|
131
|
+
});
|
|
132
|
+
const resourceList = (ids) => __async(void 0, null, function* () {
|
|
133
|
+
if (ids.length === 0)
|
|
134
|
+
return {
|
|
135
|
+
data: []
|
|
136
|
+
};
|
|
137
|
+
const params = new URLSearchParams();
|
|
138
|
+
ids.forEach((id, index) => params.append(`ids[${index}]`, id));
|
|
139
|
+
const url = new URL(`/search?${params.toString()}`, baseUrl);
|
|
140
|
+
const res = yield fetch(`${url}`);
|
|
141
|
+
const data = yield res.json();
|
|
142
|
+
return data;
|
|
143
|
+
});
|
|
144
|
+
return {
|
|
145
|
+
search,
|
|
146
|
+
related,
|
|
147
|
+
resource,
|
|
148
|
+
resourceList
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
var formatServiceAddress = (address) => {
|
|
152
|
+
const street1 = address.street1 ? address.street1 + ", " : "";
|
|
153
|
+
const street2 = address.street2 ? address.street2 + ", " : "";
|
|
154
|
+
const city = address.city ? address.city + ", " : "";
|
|
155
|
+
const province = address.province ? address.province + ", " : "";
|
|
156
|
+
const postalCode = address.postalCode ? address.postalCode : "";
|
|
157
|
+
const newAddress = street1 + street2 + city + province + postalCode;
|
|
158
|
+
if (newAddress.endsWith(", ")) {
|
|
159
|
+
return newAddress.slice(0, -2);
|
|
160
|
+
} else
|
|
161
|
+
return newAddress;
|
|
162
|
+
};
|
|
163
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
164
|
+
0 && (module.exports = {
|
|
165
|
+
CordsAPI,
|
|
166
|
+
ResourceOptions,
|
|
167
|
+
formatServiceAddress
|
|
168
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
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 baseUrl = "https://api.cords.ai";
|
|
44
|
+
var CordsAPI = ({ apiKey }) => {
|
|
45
|
+
const request = (input, init) => __async(void 0, null, function* () {
|
|
46
|
+
const res = yield fetch(input, __spreadProps(__spreadValues({}, init), {
|
|
47
|
+
headers: __spreadValues({
|
|
48
|
+
"x-api-key": apiKey
|
|
49
|
+
}, init == null ? void 0 : init.headers)
|
|
50
|
+
}));
|
|
51
|
+
if (!res.ok) {
|
|
52
|
+
if (res.status === 403)
|
|
53
|
+
throw new Error("Bad API key. Ensure you have a valid API key.");
|
|
54
|
+
const data = yield res.json();
|
|
55
|
+
if (data.detail)
|
|
56
|
+
throw new Error(data.detail);
|
|
57
|
+
else
|
|
58
|
+
throw new Error("An error occurred");
|
|
59
|
+
}
|
|
60
|
+
return res;
|
|
61
|
+
});
|
|
62
|
+
const search = (q, options) => __async(void 0, null, function* () {
|
|
63
|
+
const url = new URL("/search", baseUrl);
|
|
64
|
+
const params = new URLSearchParams({
|
|
65
|
+
q
|
|
66
|
+
});
|
|
67
|
+
if ((options == null ? void 0 : options.page) !== void 0)
|
|
68
|
+
params.append("page", options.page.toString());
|
|
69
|
+
if ((options == null ? void 0 : options.lat) !== void 0)
|
|
70
|
+
params.append("lat", options.lat.toString());
|
|
71
|
+
if ((options == null ? void 0 : options.lng) !== void 0)
|
|
72
|
+
params.append("lng", options.lng.toString());
|
|
73
|
+
if ((options == null ? void 0 : options.distance) !== void 0)
|
|
74
|
+
params.append("distance", options.distance.toString());
|
|
75
|
+
if ((options == null ? void 0 : options.filter) !== void 0) {
|
|
76
|
+
for (const [key, value] of Object.entries(options.filter)) {
|
|
77
|
+
if (value)
|
|
78
|
+
params.append(`filter[${key}]`, "true");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const res = yield request(`${url}?${params}`);
|
|
82
|
+
const data = yield res.json();
|
|
83
|
+
return data;
|
|
84
|
+
});
|
|
85
|
+
const related = (id) => __async(void 0, null, function* () {
|
|
86
|
+
const url = new URL(`/resource/${id}/related`, baseUrl);
|
|
87
|
+
const res = yield request(`${url}`);
|
|
88
|
+
if (!res.ok) {
|
|
89
|
+
const data2 = yield res.json();
|
|
90
|
+
throw new Error(data2.detail);
|
|
91
|
+
}
|
|
92
|
+
const data = yield res.json();
|
|
93
|
+
return data;
|
|
94
|
+
});
|
|
95
|
+
const resource = (id) => __async(void 0, null, function* () {
|
|
96
|
+
const url = new URL(`/resource/${id}`, baseUrl);
|
|
97
|
+
const res = yield fetch(url, {
|
|
98
|
+
headers: {
|
|
99
|
+
"x-api-key": apiKey
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
if (!res.ok) {
|
|
103
|
+
const data2 = yield res.json();
|
|
104
|
+
throw new Error(data2.detail);
|
|
105
|
+
}
|
|
106
|
+
const data = yield res.json();
|
|
107
|
+
return data;
|
|
108
|
+
});
|
|
109
|
+
const resourceList = (ids) => __async(void 0, null, function* () {
|
|
110
|
+
if (ids.length === 0)
|
|
111
|
+
return {
|
|
112
|
+
data: []
|
|
113
|
+
};
|
|
114
|
+
const params = new URLSearchParams();
|
|
115
|
+
ids.forEach((id, index) => params.append(`ids[${index}]`, id));
|
|
116
|
+
const url = new URL(`/search?${params.toString()}`, baseUrl);
|
|
117
|
+
const res = yield fetch(`${url}`);
|
|
118
|
+
const data = yield res.json();
|
|
119
|
+
return data;
|
|
120
|
+
});
|
|
121
|
+
return {
|
|
122
|
+
search,
|
|
123
|
+
related,
|
|
124
|
+
resource,
|
|
125
|
+
resourceList
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
var formatServiceAddress = (address) => {
|
|
129
|
+
const street1 = address.street1 ? address.street1 + ", " : "";
|
|
130
|
+
const street2 = address.street2 ? address.street2 + ", " : "";
|
|
131
|
+
const city = address.city ? address.city + ", " : "";
|
|
132
|
+
const province = address.province ? address.province + ", " : "";
|
|
133
|
+
const postalCode = address.postalCode ? address.postalCode : "";
|
|
134
|
+
const newAddress = street1 + street2 + city + province + postalCode;
|
|
135
|
+
if (newAddress.endsWith(", ")) {
|
|
136
|
+
return newAddress.slice(0, -2);
|
|
137
|
+
} else
|
|
138
|
+
return newAddress;
|
|
139
|
+
};
|
|
140
|
+
export {
|
|
141
|
+
CordsAPI,
|
|
142
|
+
ResourceOptions,
|
|
143
|
+
formatServiceAddress
|
|
144
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cords/sdk",
|
|
3
|
+
"main": "dist/index.js",
|
|
4
|
+
"module": "dist/index.mjs",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"version": "0.0.1",
|
|
7
|
+
"private": false,
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
13
|
+
"dev": "bun run build -- --watch"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/bun": "latest"
|
|
17
|
+
},
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"typescript": "^5.0.0"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"tsup": "^8.0.2"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { ResourceAddressType } from "../dist";
|
|
2
|
+
import type { CordsError, ResourceType, SearchOptions } from "./types";
|
|
3
|
+
export * from "./types";
|
|
4
|
+
|
|
5
|
+
export const ResourceOptions = {};
|
|
6
|
+
|
|
7
|
+
const baseUrl = "https://api.cords.ai";
|
|
8
|
+
|
|
9
|
+
export const CordsAPI = ({ apiKey }: { apiKey: string }) => {
|
|
10
|
+
const request = async (input: RequestInfo, init?: RequestInit) => {
|
|
11
|
+
const res = await fetch(input, {
|
|
12
|
+
...init,
|
|
13
|
+
headers: {
|
|
14
|
+
"x-api-key": apiKey,
|
|
15
|
+
...init?.headers,
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
if (!res.ok) {
|
|
19
|
+
if (res.status === 403)
|
|
20
|
+
throw new Error("Bad API key. Ensure you have a valid API key.");
|
|
21
|
+
const data: CordsError = await res.json();
|
|
22
|
+
if (data.detail) throw new Error(data.detail);
|
|
23
|
+
else throw new Error("An error occurred");
|
|
24
|
+
}
|
|
25
|
+
return res;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const search = async (q: string, options?: SearchOptions) => {
|
|
29
|
+
const url = new URL("/search", baseUrl);
|
|
30
|
+
const params = new URLSearchParams({
|
|
31
|
+
q,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Add top-level parameters
|
|
35
|
+
if (options?.page !== undefined) params.append("page", options.page.toString());
|
|
36
|
+
if (options?.lat !== undefined) params.append("lat", options.lat.toString());
|
|
37
|
+
if (options?.lng !== undefined) params.append("lng", options.lng.toString());
|
|
38
|
+
if (options?.distance !== undefined) params.append("distance", options.distance.toString());
|
|
39
|
+
|
|
40
|
+
// Add filter parameters
|
|
41
|
+
if (options?.filter !== undefined) {
|
|
42
|
+
for (const [key, value] of Object.entries(options.filter)) {
|
|
43
|
+
if (value) params.append(`filter[${key}]`, "true");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const res = await request(`${url}?${params}`);
|
|
48
|
+
const data = await res.json();
|
|
49
|
+
return data as { data: ResourceType[] };
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const related = async (id: string) => {
|
|
53
|
+
const url = new URL(`/resource/${id}/related`, baseUrl);
|
|
54
|
+
|
|
55
|
+
const res = await request(`${url}`);
|
|
56
|
+
if (!res.ok) {
|
|
57
|
+
const data: CordsError = await res.json();
|
|
58
|
+
throw new Error(data.detail);
|
|
59
|
+
}
|
|
60
|
+
const data = await res.json();
|
|
61
|
+
return data as { data: ResourceType[] };
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const resource = async (id: string) => {
|
|
65
|
+
const url = new URL(`/resource/${id}`, baseUrl);
|
|
66
|
+
|
|
67
|
+
const res = await fetch(url, {
|
|
68
|
+
headers: {
|
|
69
|
+
"x-api-key": apiKey,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
if (!res.ok) {
|
|
73
|
+
const data: CordsError = await res.json();
|
|
74
|
+
throw new Error(data.detail);
|
|
75
|
+
}
|
|
76
|
+
const data = await res.json();
|
|
77
|
+
return data as ResourceType;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const resourceList = async (ids: string[]): Promise<{ data: ResourceType[] }> => {
|
|
81
|
+
if (ids.length === 0)
|
|
82
|
+
return {
|
|
83
|
+
data: [],
|
|
84
|
+
};
|
|
85
|
+
const params = new URLSearchParams();
|
|
86
|
+
ids.forEach((id, index) => params.append(`ids[${index}]`, id));
|
|
87
|
+
|
|
88
|
+
const url = new URL(`/search?${params.toString()}`, baseUrl);
|
|
89
|
+
|
|
90
|
+
const res = await fetch(`${url}`);
|
|
91
|
+
const data = await res.json();
|
|
92
|
+
return data as { data: ResourceType[] };
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
search,
|
|
97
|
+
related,
|
|
98
|
+
resource,
|
|
99
|
+
resourceList,
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export const formatServiceAddress = (address: ResourceAddressType) => {
|
|
104
|
+
const street1 = address.street1 ? address.street1 + ", " : "";
|
|
105
|
+
const street2 = address.street2 ? address.street2 + ", " : "";
|
|
106
|
+
const city = address.city ? address.city + ", " : "";
|
|
107
|
+
const province = address.province ? address.province + ", " : "";
|
|
108
|
+
const postalCode = address.postalCode ? address.postalCode : "";
|
|
109
|
+
const newAddress = street1 + street2 + city + province + postalCode;
|
|
110
|
+
if (newAddress.endsWith(", ")) {
|
|
111
|
+
return newAddress.slice(0, -2);
|
|
112
|
+
} else return newAddress;
|
|
113
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
type LocalizedFieldType = {
|
|
2
|
+
en: string;
|
|
3
|
+
fr: string;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type ResourceAddressType = {
|
|
7
|
+
street1: string;
|
|
8
|
+
street2: string;
|
|
9
|
+
city: string;
|
|
10
|
+
postalCode: string;
|
|
11
|
+
province: string;
|
|
12
|
+
country: string;
|
|
13
|
+
lat: number | null;
|
|
14
|
+
lng: number | null;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type ResourceType = {
|
|
18
|
+
id: string;
|
|
19
|
+
name: LocalizedFieldType;
|
|
20
|
+
description: LocalizedFieldType;
|
|
21
|
+
website: LocalizedFieldType;
|
|
22
|
+
email: LocalizedFieldType;
|
|
23
|
+
address: ResourceAddressType;
|
|
24
|
+
addresses: ResourceAddressType[];
|
|
25
|
+
phoneNumbers: {
|
|
26
|
+
phone: string;
|
|
27
|
+
name: string;
|
|
28
|
+
type: string;
|
|
29
|
+
}[];
|
|
30
|
+
partner: string;
|
|
31
|
+
delivery: "national" | "provincial" | "local" | "regional" | null;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type SearchOptions = {
|
|
35
|
+
page?: number;
|
|
36
|
+
lat?: number;
|
|
37
|
+
lng?: number;
|
|
38
|
+
distance?: number;
|
|
39
|
+
pageSize?: number;
|
|
40
|
+
filter?: {
|
|
41
|
+
"211"?: boolean;
|
|
42
|
+
mentor?: boolean;
|
|
43
|
+
prosper?: boolean;
|
|
44
|
+
magnet?: boolean;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export type CordsError = {
|
|
49
|
+
detail: string;
|
|
50
|
+
status: number;
|
|
51
|
+
title: string;
|
|
52
|
+
type: string;
|
|
53
|
+
};
|
package/tsconfig.json
ADDED