@cords/sdk 0.0.3 → 0.0.4

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.
@@ -0,0 +1,106 @@
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
+ filter?: {
64
+ "211"?: boolean;
65
+ mentor?: boolean;
66
+ prosper?: boolean;
67
+ magnet?: boolean;
68
+ };
69
+ };
70
+ type CordsError = {
71
+ detail: string;
72
+ status: number;
73
+ title: string;
74
+ type: string;
75
+ };
76
+
77
+ declare const ResourceOptions: {};
78
+ declare const CordsAPI: ({ apiKey, version, }: {
79
+ apiKey: string;
80
+ version?: "production" | "dev";
81
+ }) => {
82
+ search: (q: string, options?: SearchOptions) => Promise<{
83
+ data: SearchResourceType[];
84
+ meta: {
85
+ total: number;
86
+ lat: number;
87
+ lng: number;
88
+ };
89
+ }>;
90
+ related: (id: string) => Promise<{
91
+ data: ResourceType[];
92
+ }>;
93
+ resource: (id: string) => Promise<ResourceType>;
94
+ resourceList: (ids: string[]) => Promise<{
95
+ data: ResourceType[];
96
+ }>;
97
+ nearestNeighbour: (id: string, options: {
98
+ lat: number;
99
+ lng: number;
100
+ }) => Promise<{
101
+ data: ResourceType[];
102
+ }>;
103
+ };
104
+ declare const formatServiceAddress: (address: ResourceAddressType) => string;
105
+
106
+ export { CordsAPI, type CordsError, type ResourceAddressType, type ResourceBodyType, ResourceOptions, type ResourceType, type SearchOptions, type SearchResourceType, formatServiceAddress };
@@ -0,0 +1,106 @@
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
+ filter?: {
64
+ "211"?: boolean;
65
+ mentor?: boolean;
66
+ prosper?: boolean;
67
+ magnet?: boolean;
68
+ };
69
+ };
70
+ type CordsError = {
71
+ detail: string;
72
+ status: number;
73
+ title: string;
74
+ type: string;
75
+ };
76
+
77
+ declare const ResourceOptions: {};
78
+ declare const CordsAPI: ({ apiKey, version, }: {
79
+ apiKey: string;
80
+ version?: "production" | "dev";
81
+ }) => {
82
+ search: (q: string, options?: SearchOptions) => Promise<{
83
+ data: SearchResourceType[];
84
+ meta: {
85
+ total: number;
86
+ lat: number;
87
+ lng: number;
88
+ };
89
+ }>;
90
+ related: (id: string) => Promise<{
91
+ data: ResourceType[];
92
+ }>;
93
+ resource: (id: string) => Promise<ResourceType>;
94
+ resourceList: (ids: string[]) => Promise<{
95
+ data: ResourceType[];
96
+ }>;
97
+ nearestNeighbour: (id: string, options: {
98
+ lat: number;
99
+ lng: number;
100
+ }) => Promise<{
101
+ data: ResourceType[];
102
+ }>;
103
+ };
104
+ declare const formatServiceAddress: (address: ResourceAddressType) => string;
105
+
106
+ export { CordsAPI, type CordsError, type ResourceAddressType, type ResourceBodyType, ResourceOptions, type ResourceType, type SearchOptions, type SearchResourceType, formatServiceAddress };
package/dist/index.js ADDED
@@ -0,0 +1,183 @@
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
+ if ((options == null ? void 0 : options.page) !== void 0)
94
+ params.append("page", options.page.toString());
95
+ if ((options == null ? void 0 : options.lat) !== void 0)
96
+ params.append("lat", options.lat.toString());
97
+ if ((options == null ? void 0 : options.lng) !== void 0)
98
+ params.append("lng", options.lng.toString());
99
+ if ((options == null ? void 0 : options.distance) !== void 0)
100
+ params.append("distance", options.distance.toString());
101
+ if ((options == null ? void 0 : options.filter) !== void 0) {
102
+ for (const [key, value] of Object.entries(options.filter)) {
103
+ if (value)
104
+ params.append(`filter[${key}]`, "true");
105
+ }
106
+ }
107
+ const res = yield request(`${url.toString()}?${params}`);
108
+ const data = yield res.json();
109
+ return data;
110
+ });
111
+ const related = (id) => __async(void 0, null, function* () {
112
+ const url = new URL(`/resource/${id}/related`, baseUrl);
113
+ const res = yield request(url.toString());
114
+ if (!res.ok) {
115
+ const data2 = yield res.json();
116
+ throw new Error(data2.detail);
117
+ }
118
+ const data = yield res.json();
119
+ return data;
120
+ });
121
+ const resource = (id) => __async(void 0, null, function* () {
122
+ const url = new URL(`/resource/${id}`, baseUrl);
123
+ const res = yield request(url.toString());
124
+ if (!res.ok) {
125
+ const data2 = yield res.json();
126
+ throw new Error(data2.detail);
127
+ }
128
+ const data = yield res.json();
129
+ return data;
130
+ });
131
+ const resourceList = (ids) => __async(void 0, null, function* () {
132
+ if (ids.length === 0)
133
+ return {
134
+ data: []
135
+ };
136
+ const params = new URLSearchParams();
137
+ ids.forEach((id, index) => params.append(`ids[${index}]`, id));
138
+ const url = new URL(`/search?${params.toString()}`, baseUrl);
139
+ const res = yield request(url.toString());
140
+ const data = yield res.json();
141
+ return data;
142
+ });
143
+ const nearestNeighbour = (id, options) => __async(void 0, null, function* () {
144
+ const url = new URL(`/resource/${id}/nearest-neighbor`, baseUrl);
145
+ const params = new URLSearchParams({
146
+ lat: options.lat.toString(),
147
+ lng: options.lng.toString()
148
+ });
149
+ const res = yield request(url.toString() + "?delivery=local&" + params.toString());
150
+ if (!res.ok) {
151
+ const data2 = yield res.json();
152
+ throw new Error(data2.detail);
153
+ }
154
+ const data = yield res.json();
155
+ console.log(data);
156
+ return data;
157
+ });
158
+ return {
159
+ search,
160
+ related,
161
+ resource,
162
+ resourceList,
163
+ nearestNeighbour
164
+ };
165
+ };
166
+ var formatServiceAddress = (address) => {
167
+ const street1 = address.street1 ? address.street1 + ", " : "";
168
+ const street2 = address.street2 ? address.street2 + ", " : "";
169
+ const city = address.city ? address.city + ", " : "";
170
+ const province = address.province ? address.province + ", " : "";
171
+ const postalCode = address.postalCode ? address.postalCode : "";
172
+ const newAddress = street1 + street2 + city + province + postalCode;
173
+ if (newAddress.endsWith(", ")) {
174
+ return newAddress.slice(0, -2);
175
+ } else
176
+ return newAddress;
177
+ };
178
+ // Annotate the CommonJS export names for ESM import in node:
179
+ 0 && (module.exports = {
180
+ CordsAPI,
181
+ ResourceOptions,
182
+ formatServiceAddress
183
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,159 @@
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
+ if ((options == null ? void 0 : options.page) !== void 0)
71
+ params.append("page", options.page.toString());
72
+ if ((options == null ? void 0 : options.lat) !== void 0)
73
+ params.append("lat", options.lat.toString());
74
+ if ((options == null ? void 0 : options.lng) !== void 0)
75
+ params.append("lng", options.lng.toString());
76
+ if ((options == null ? void 0 : options.distance) !== void 0)
77
+ params.append("distance", options.distance.toString());
78
+ if ((options == null ? void 0 : options.filter) !== void 0) {
79
+ for (const [key, value] of Object.entries(options.filter)) {
80
+ if (value)
81
+ params.append(`filter[${key}]`, "true");
82
+ }
83
+ }
84
+ const res = yield request(`${url.toString()}?${params}`);
85
+ const data = yield res.json();
86
+ return data;
87
+ });
88
+ const related = (id) => __async(void 0, null, function* () {
89
+ const url = new URL(`/resource/${id}/related`, baseUrl);
90
+ const res = yield request(url.toString());
91
+ if (!res.ok) {
92
+ const data2 = yield res.json();
93
+ throw new Error(data2.detail);
94
+ }
95
+ const data = yield res.json();
96
+ return data;
97
+ });
98
+ const resource = (id) => __async(void 0, null, function* () {
99
+ const url = new URL(`/resource/${id}`, baseUrl);
100
+ const res = yield request(url.toString());
101
+ if (!res.ok) {
102
+ const data2 = yield res.json();
103
+ throw new Error(data2.detail);
104
+ }
105
+ const data = yield res.json();
106
+ return data;
107
+ });
108
+ const resourceList = (ids) => __async(void 0, null, function* () {
109
+ if (ids.length === 0)
110
+ return {
111
+ data: []
112
+ };
113
+ const params = new URLSearchParams();
114
+ ids.forEach((id, index) => params.append(`ids[${index}]`, id));
115
+ const url = new URL(`/search?${params.toString()}`, baseUrl);
116
+ const res = yield request(url.toString());
117
+ const data = yield res.json();
118
+ return data;
119
+ });
120
+ const nearestNeighbour = (id, options) => __async(void 0, null, function* () {
121
+ const url = new URL(`/resource/${id}/nearest-neighbor`, baseUrl);
122
+ const params = new URLSearchParams({
123
+ lat: options.lat.toString(),
124
+ lng: options.lng.toString()
125
+ });
126
+ const res = yield request(url.toString() + "?delivery=local&" + params.toString());
127
+ if (!res.ok) {
128
+ const data2 = yield res.json();
129
+ throw new Error(data2.detail);
130
+ }
131
+ const data = yield res.json();
132
+ console.log(data);
133
+ return data;
134
+ });
135
+ return {
136
+ search,
137
+ related,
138
+ resource,
139
+ resourceList,
140
+ nearestNeighbour
141
+ };
142
+ };
143
+ var formatServiceAddress = (address) => {
144
+ const street1 = address.street1 ? address.street1 + ", " : "";
145
+ const street2 = address.street2 ? address.street2 + ", " : "";
146
+ const city = address.city ? address.city + ", " : "";
147
+ const province = address.province ? address.province + ", " : "";
148
+ const postalCode = address.postalCode ? address.postalCode : "";
149
+ const newAddress = street1 + street2 + city + province + postalCode;
150
+ if (newAddress.endsWith(", ")) {
151
+ return newAddress.slice(0, -2);
152
+ } else
153
+ return newAddress;
154
+ };
155
+ export {
156
+ CordsAPI,
157
+ ResourceOptions,
158
+ formatServiceAddress
159
+ };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "main": "./dist/index.js",
4
4
  "module": "./dist/index.mjs",
5
5
  "types": "./dist/index.d.ts",
6
- "version": "0.0.3",
6
+ "version": "0.0.4",
7
7
  "private": false,
8
8
  "publishConfig": {
9
9
  "access": "public"
package/src/index.ts CHANGED
@@ -1,11 +1,14 @@
1
- import { SearchResourceType } from "../dist";
2
- import type { CordsError, ResourceAddressType, ResourceType, SearchOptions } from "./types";
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",
@@ -119,6 +122,7 @@ export const CordsAPI = ({
119
122
  throw new Error(data.detail);
120
123
  }
121
124
  const data = await res.json();
125
+ console.log(data);
122
126
  return data as { data: ResourceType[] };
123
127
  };
124
128
 
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<