@parseapi/sdk 0.1.0
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/LICENSE +21 -0
- package/README.md +97 -0
- package/dist/index.cjs +200 -0
- package/dist/index.d.cts +553 -0
- package/dist/index.d.ts +553 -0
- package/dist/index.js +174 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 parseAPI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# @parseapi/sdk
|
|
2
|
+
|
|
3
|
+
Official parseAPI client for Node and TypeScript.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @parseapi/sdk
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { parseAPI } from '@parseapi/sdk';
|
|
11
|
+
|
|
12
|
+
const parse = parseAPI('your-api-key');
|
|
13
|
+
const country = await parse.country('US');
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Get a key at [parseapi.com](https://parseapi.com). The client also reads `PARSEAPI_KEY` from the environment.
|
|
17
|
+
|
|
18
|
+
## Calls
|
|
19
|
+
|
|
20
|
+
One method per endpoint, named after the route.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
await parse.ip('8.8.8.8');
|
|
24
|
+
await parse.ip.self();
|
|
25
|
+
await parse.email('hello@gmail.com');
|
|
26
|
+
await parse.phone('+14155552671');
|
|
27
|
+
await parse.postal('28202', { country: 'US' });
|
|
28
|
+
await parse.postal.nearby('28202', { country: 'US', radius: 40 });
|
|
29
|
+
await parse.postal.distance('28202', '10001', { country: 'US' });
|
|
30
|
+
await parse.city('charlotte', { country: 'US' });
|
|
31
|
+
await parse.city.id('city_mb8mbqrkz8zb');
|
|
32
|
+
await parse.city.search('char', { country: 'US', limit: 10 });
|
|
33
|
+
await parse.city.nearest(35.2271, -80.8431);
|
|
34
|
+
await parse.country('US');
|
|
35
|
+
await parse.country.states('US');
|
|
36
|
+
await parse.state('NC', { country: 'US' });
|
|
37
|
+
await parse.state.districts('NC', { country: 'US' });
|
|
38
|
+
await parse.district('37081');
|
|
39
|
+
await parse.continent('NA');
|
|
40
|
+
await parse.continent.countries('NA');
|
|
41
|
+
await parse.currency('USD');
|
|
42
|
+
await parse.currency.rate('USD', 'EUR');
|
|
43
|
+
await parse.language('en');
|
|
44
|
+
await parse.timezone('America/New_York');
|
|
45
|
+
await parse.holiday('US', { year: 2026 });
|
|
46
|
+
await parse.holiday.date('US', '2026-12-25');
|
|
47
|
+
await parse.elevation(35.2271, -80.8431);
|
|
48
|
+
await parse.point(36.0726, -79.792);
|
|
49
|
+
await parse.weather(40.7128, -74.006);
|
|
50
|
+
await parse.domain('example.com');
|
|
51
|
+
await parse.mx('example.com');
|
|
52
|
+
await parse.useragent(uaString);
|
|
53
|
+
await parse.emoji('rocket');
|
|
54
|
+
await parse.emoji.search('fire');
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Every response is fully typed.
|
|
58
|
+
|
|
59
|
+
## Deep
|
|
60
|
+
|
|
61
|
+
Pass `deep: true` to include the nested `deep` object with richer fields.
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
const ip = await parse.ip('52.94.76.10', { deep: true });
|
|
65
|
+
ip.deep?.datacenter; // true
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Errors
|
|
69
|
+
|
|
70
|
+
Every non-2xx response throws a `ParseAPIError` with `status`, `code`, `docs`, and `requestId`. Branch on `code`.
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { ParseAPIError } from '@parseapi/sdk';
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
await parse.city('atlantis');
|
|
77
|
+
} catch (err) {
|
|
78
|
+
if (err instanceof ParseAPIError && err.code === 'not_found') {
|
|
79
|
+
// no such city
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Options
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const parse = parseAPI('your-api-key', {
|
|
88
|
+
timeoutMs: 10000, // per-attempt timeout
|
|
89
|
+
retries: 2, // automatic retries on network errors, 429, and 5xx
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Requires Node 18 or later. Zero dependencies.
|
|
94
|
+
|
|
95
|
+
## Docs
|
|
96
|
+
|
|
97
|
+
Full field reference for every endpoint: [parseapi.com/docs](https://parseapi.com/docs)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
ParseAPIError: () => ParseAPIError,
|
|
24
|
+
parseAPI: () => parseAPI
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
var VERSION = "0.1.0";
|
|
28
|
+
var DEFAULT_BASE_URL = "https://api.parseapi.com";
|
|
29
|
+
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
30
|
+
var DEFAULT_RETRIES = 2;
|
|
31
|
+
var RETRY_STATUS = /* @__PURE__ */ new Set([429, 500, 502, 503, 504]);
|
|
32
|
+
var RETRY_AFTER_CAP_MS = 5e3;
|
|
33
|
+
var ParseAPIError = class extends Error {
|
|
34
|
+
/** HTTP status */
|
|
35
|
+
status;
|
|
36
|
+
/** Machine-readable error code, e.g. 'not_found', 'invalid_api_key', 'rate_limited' */
|
|
37
|
+
code;
|
|
38
|
+
/** Link to the docs section for this error */
|
|
39
|
+
docs;
|
|
40
|
+
/** Send this if you contact support */
|
|
41
|
+
requestId;
|
|
42
|
+
constructor(status, code, message, docs, requestId) {
|
|
43
|
+
super(message);
|
|
44
|
+
this.name = "ParseAPIError";
|
|
45
|
+
this.status = status;
|
|
46
|
+
this.code = code;
|
|
47
|
+
this.docs = docs;
|
|
48
|
+
this.requestId = requestId;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
function env(name) {
|
|
52
|
+
return typeof process !== "undefined" ? process.env?.[name] : void 0;
|
|
53
|
+
}
|
|
54
|
+
function sleep(ms) {
|
|
55
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
56
|
+
}
|
|
57
|
+
function retryDelayMs(attempt, retryAfter) {
|
|
58
|
+
if (retryAfter) {
|
|
59
|
+
const seconds = Number(retryAfter);
|
|
60
|
+
if (Number.isFinite(seconds) && seconds >= 0) {
|
|
61
|
+
return Math.min(seconds * 1e3, RETRY_AFTER_CAP_MS);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return Math.random() * 250 * 2 ** attempt;
|
|
65
|
+
}
|
|
66
|
+
function parseAPI(apiKey, options = {}) {
|
|
67
|
+
const key = apiKey ?? env("PARSEAPI_KEY");
|
|
68
|
+
if (!key) {
|
|
69
|
+
throw new Error("parseAPI: missing API key. Pass one or set PARSEAPI_KEY.");
|
|
70
|
+
}
|
|
71
|
+
const baseUrl = (options.baseUrl ?? env("PARSEAPI_BASE_URL") ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
72
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
73
|
+
const retries = options.retries ?? DEFAULT_RETRIES;
|
|
74
|
+
const doFetch = options.fetch ?? fetch;
|
|
75
|
+
async function request(path, query, headers) {
|
|
76
|
+
const url = new URL(baseUrl + path);
|
|
77
|
+
for (const [name, value] of Object.entries(query ?? {})) {
|
|
78
|
+
if (value !== void 0) url.searchParams.set(name, String(value));
|
|
79
|
+
}
|
|
80
|
+
for (let attempt = 0; ; attempt++) {
|
|
81
|
+
let res;
|
|
82
|
+
try {
|
|
83
|
+
res = await doFetch(url, {
|
|
84
|
+
headers: {
|
|
85
|
+
"X-API-Key": key,
|
|
86
|
+
"User-Agent": `parseapi-node/${VERSION}`,
|
|
87
|
+
...headers
|
|
88
|
+
},
|
|
89
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
90
|
+
});
|
|
91
|
+
} catch (err) {
|
|
92
|
+
if (attempt < retries) {
|
|
93
|
+
await sleep(retryDelayMs(attempt, null));
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
throw err;
|
|
97
|
+
}
|
|
98
|
+
if (res.ok) {
|
|
99
|
+
return await res.json();
|
|
100
|
+
}
|
|
101
|
+
if (RETRY_STATUS.has(res.status) && attempt < retries) {
|
|
102
|
+
await sleep(retryDelayMs(attempt, res.headers.get("Retry-After")));
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
let body = {};
|
|
106
|
+
try {
|
|
107
|
+
body = await res.json();
|
|
108
|
+
} catch {
|
|
109
|
+
}
|
|
110
|
+
throw new ParseAPIError(
|
|
111
|
+
res.status,
|
|
112
|
+
typeof body.code === "string" ? body.code : "unknown_error",
|
|
113
|
+
typeof body.message === "string" ? body.message : `Request failed with status ${res.status}`,
|
|
114
|
+
typeof body.docs === "string" ? body.docs : null,
|
|
115
|
+
typeof body.request_id === "string" ? body.request_id : null
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const enc = encodeURIComponent;
|
|
120
|
+
const deepQuery = (opts) => opts?.deep ? { deep: true } : {};
|
|
121
|
+
return {
|
|
122
|
+
ip: Object.assign(
|
|
123
|
+
(ip, opts) => request(`/ip/${enc(ip)}`, deepQuery(opts)),
|
|
124
|
+
{
|
|
125
|
+
self: (opts) => request("/ip", deepQuery(opts))
|
|
126
|
+
}
|
|
127
|
+
),
|
|
128
|
+
continent: Object.assign(
|
|
129
|
+
(code) => request(`/continent/${enc(code)}`),
|
|
130
|
+
{
|
|
131
|
+
countries: (code) => request(`/continent/${enc(code)}/countries`)
|
|
132
|
+
}
|
|
133
|
+
),
|
|
134
|
+
country: Object.assign(
|
|
135
|
+
(code) => request(`/country/${enc(code)}`),
|
|
136
|
+
{
|
|
137
|
+
states: (code) => request(`/country/${enc(code)}/states`)
|
|
138
|
+
}
|
|
139
|
+
),
|
|
140
|
+
state: Object.assign(
|
|
141
|
+
(code, opts) => request(`/state/${enc(code)}`, { country: opts.country }),
|
|
142
|
+
{
|
|
143
|
+
districts: (code, opts) => request(`/state/${enc(code)}/districts`, { country: opts.country })
|
|
144
|
+
}
|
|
145
|
+
),
|
|
146
|
+
district: (code, opts) => request(`/district/${enc(code)}`, { country: opts?.country }),
|
|
147
|
+
city: Object.assign(
|
|
148
|
+
(name, opts) => request(`/city/${enc(name)}`, { country: opts?.country, state: opts?.state }),
|
|
149
|
+
{
|
|
150
|
+
id: (id) => request(`/city/id/${enc(id)}`),
|
|
151
|
+
search: (q, opts) => request("/city", { q, country: opts?.country, state: opts?.state, limit: opts?.limit }),
|
|
152
|
+
nearest: (lat, lon) => request("/city", { lat, lon })
|
|
153
|
+
}
|
|
154
|
+
),
|
|
155
|
+
postal: Object.assign(
|
|
156
|
+
(code, opts) => request(`/postal/${enc(code)}`, { country: opts.country }),
|
|
157
|
+
{
|
|
158
|
+
nearby: (code, opts) => request(`/postal/${enc(code)}/nearby`, {
|
|
159
|
+
country: opts.country,
|
|
160
|
+
radius: opts.radius,
|
|
161
|
+
unit: opts.unit
|
|
162
|
+
}),
|
|
163
|
+
distance: (from, to, opts) => request(`/postal/${enc(from)}/distance/${enc(to)}`, { country: opts.country })
|
|
164
|
+
}
|
|
165
|
+
),
|
|
166
|
+
email: (email, opts) => request(`/email/${enc(email)}`, deepQuery(opts)),
|
|
167
|
+
phone: (number, opts) => request(`/phone/${enc(number)}`, { country: opts?.country, ...deepQuery(opts) }),
|
|
168
|
+
domain: (domain, opts) => request(`/domain/${enc(domain)}`, deepQuery(opts)),
|
|
169
|
+
mx: (domain) => request(`/mx/${enc(domain)}`),
|
|
170
|
+
useragent: (ua, opts) => request("/useragent", deepQuery(opts), { "User-Agent": ua }),
|
|
171
|
+
currency: Object.assign(
|
|
172
|
+
(code) => request(`/currency/${enc(code)}`),
|
|
173
|
+
{
|
|
174
|
+
rate: (base, quote) => request(`/currency/${enc(base)}/${enc(quote)}`)
|
|
175
|
+
}
|
|
176
|
+
),
|
|
177
|
+
language: (code) => request(`/language/${enc(code)}`),
|
|
178
|
+
timezone: (id, opts) => request(`/timezone/${enc(id)}`, { at: opts?.at }),
|
|
179
|
+
holiday: Object.assign(
|
|
180
|
+
(country, opts) => request(`/holiday/${enc(country)}`, { year: opts?.year }),
|
|
181
|
+
{
|
|
182
|
+
date: (country, date) => request(`/holiday/${enc(country)}/${enc(date)}`)
|
|
183
|
+
}
|
|
184
|
+
),
|
|
185
|
+
elevation: (lat, lon) => request("/elevation", { lat, lon }),
|
|
186
|
+
point: (lat, lon, opts) => request("/point", { lat, lon, ...deepQuery(opts) }),
|
|
187
|
+
weather: (lat, lon, opts) => request("/weather", { lat, lon, ...deepQuery(opts) }),
|
|
188
|
+
emoji: Object.assign(
|
|
189
|
+
(emoji) => request(`/emoji/${enc(emoji)}`),
|
|
190
|
+
{
|
|
191
|
+
search: (q, opts) => request("/emoji", { q, limit: opts?.limit })
|
|
192
|
+
}
|
|
193
|
+
)
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
197
|
+
0 && (module.exports = {
|
|
198
|
+
ParseAPIError,
|
|
199
|
+
parseAPI
|
|
200
|
+
});
|