@cords/sdk 0.0.1 → 0.0.2

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/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
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",
3
+ "main": "./dist/index.js",
4
+ "module": "./dist/index.mjs",
5
+ "types": "./dist/index.d.ts",
6
+ "version": "0.0.2",
7
7
  "private": false,
8
8
  "publishConfig": {
9
9
  "access": "public"
package/src/index.ts CHANGED
@@ -1,12 +1,20 @@
1
- import { ResourceAddressType } from "../dist";
2
- import type { CordsError, ResourceType, SearchOptions } from "./types";
1
+ import { SearchResourceType } from "../dist";
2
+ import type { CordsError, ResourceAddressType, ResourceType, SearchOptions } from "./types";
3
3
  export * from "./types";
4
4
 
5
5
  export const ResourceOptions = {};
6
6
 
7
7
  const baseUrl = "https://api.cords.ai";
8
8
 
9
- export const CordsAPI = ({ apiKey }: { apiKey: string }) => {
9
+ export const CordsAPI = ({
10
+ apiKey,
11
+ version = "production",
12
+ }: {
13
+ apiKey: string;
14
+ version?: "production" | "dev";
15
+ }) => {
16
+ const baseUrl = version === "production" ? "https://api.cords.ai" : "https://api.cords.dev";
17
+
10
18
  const request = async (input: RequestInfo, init?: RequestInit) => {
11
19
  const res = await fetch(input, {
12
20
  ...init,
@@ -44,15 +52,18 @@ export const CordsAPI = ({ apiKey }: { apiKey: string }) => {
44
52
  }
45
53
  }
46
54
 
47
- const res = await request(`${url}?${params}`);
55
+ const res = await request(`${url.toString()}?${params}`);
48
56
  const data = await res.json();
49
- return data as { data: ResourceType[] };
57
+ return data as {
58
+ data: SearchResourceType[];
59
+ meta: { total: number; lat: number; lng: number };
60
+ };
50
61
  };
51
62
 
52
63
  const related = async (id: string) => {
53
64
  const url = new URL(`/resource/${id}/related`, baseUrl);
54
65
 
55
- const res = await request(`${url}`);
66
+ const res = await request(url.toString());
56
67
  if (!res.ok) {
57
68
  const data: CordsError = await res.json();
58
69
  throw new Error(data.detail);
@@ -64,11 +75,7 @@ export const CordsAPI = ({ apiKey }: { apiKey: string }) => {
64
75
  const resource = async (id: string) => {
65
76
  const url = new URL(`/resource/${id}`, baseUrl);
66
77
 
67
- const res = await fetch(url, {
68
- headers: {
69
- "x-api-key": apiKey,
70
- },
71
- });
78
+ const res = await request(url.toString());
72
79
  if (!res.ok) {
73
80
  const data: CordsError = await res.json();
74
81
  throw new Error(data.detail);
@@ -87,7 +94,30 @@ export const CordsAPI = ({ apiKey }: { apiKey: string }) => {
87
94
 
88
95
  const url = new URL(`/search?${params.toString()}`, baseUrl);
89
96
 
90
- const res = await fetch(`${url}`);
97
+ const res = await request(url.toString());
98
+ const data = await res.json();
99
+ return data as { data: ResourceType[] };
100
+ };
101
+
102
+ const nearestNeighbour = async (
103
+ id: string,
104
+ options: {
105
+ lat: number;
106
+ lng: number;
107
+ }
108
+ ) => {
109
+ const url = new URL(`/resource/${id}/nearest-neighbor`, baseUrl);
110
+
111
+ const params = new URLSearchParams({
112
+ lat: options.lat.toString(),
113
+ lng: options.lng.toString(),
114
+ });
115
+
116
+ const res = await request(url.toString() + "?delivery=local&" + params.toString());
117
+ if (!res.ok) {
118
+ const data: CordsError = await res.json();
119
+ throw new Error(data.detail);
120
+ }
91
121
  const data = await res.json();
92
122
  return data as { data: ResourceType[] };
93
123
  };
@@ -97,6 +127,7 @@ export const CordsAPI = ({ apiKey }: { apiKey: string }) => {
97
127
  related,
98
128
  resource,
99
129
  resourceList,
130
+ nearestNeighbour,
100
131
  };
101
132
  };
102
133
 
package/src/types.ts CHANGED
@@ -14,6 +14,23 @@ export type ResourceAddressType = {
14
14
  lng: number | null;
15
15
  };
16
16
 
17
+ export type ResourceBodyType = {
18
+ fees: string;
19
+ hours: string;
20
+ topics: string;
21
+ twitter: string | null;
22
+ youtube: string | null;
23
+ facebook: string | null;
24
+ linkedin: string | null;
25
+ instagram: string | null;
26
+ languages: string;
27
+ eligibility: string;
28
+ recordOwner: string;
29
+ accessibility: string;
30
+ documentsRequired: string;
31
+ applicationProcess: string;
32
+ };
33
+
17
34
  export type ResourceType = {
18
35
  id: string;
19
36
  name: LocalizedFieldType;
@@ -29,12 +46,21 @@ export type ResourceType = {
29
46
  }[];
30
47
  partner: string;
31
48
  delivery: "national" | "provincial" | "local" | "regional" | null;
49
+ body: {
50
+ en: ResourceBodyType | null;
51
+ fr: ResourceBodyType | null;
52
+ };
32
53
  };
33
54
 
55
+ export type SearchResourceType = Omit<
56
+ ResourceType,
57
+ "website" | "email" | "phoneNumbers" | "addresses" | "address"
58
+ >;
59
+
34
60
  export type SearchOptions = {
61
+ lat: number;
62
+ lng: number;
35
63
  page?: number;
36
- lat?: number;
37
- lng?: number;
38
64
  distance?: number;
39
65
  pageSize?: number;
40
66
  filter?: {
package/tsconfig.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "target": "ES2016",
4
- "module": "commonjs",
4
+ "module": "CommonJS",
5
5
  "esModuleInterop": true,
6
6
  "forceConsistentCasingInFileNames": true,
7
7
  "strict": true,
8
8
  "skipLibCheck": true,
9
- "noUncheckedIndexedAccess": true,
10
- "noEmit": true
9
+ "noUncheckedIndexedAccess": true
11
10
  }
12
11
  }
package/dist/index.d.mts DELETED
@@ -1,79 +0,0 @@
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 DELETED
@@ -1,79 +0,0 @@
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 DELETED
@@ -1,168 +0,0 @@
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 DELETED
@@ -1,144 +0,0 @@
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
- };