@atproto-labs/fetch 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/CHANGELOG.md +10 -0
- package/LICENSE.txt +7 -0
- package/dist/fetch-error.d.ts +16 -0
- package/dist/fetch-error.d.ts.map +1 -0
- package/dist/fetch-error.js +73 -0
- package/dist/fetch-error.js.map +1 -0
- package/dist/fetch-request.d.ts +7 -0
- package/dist/fetch-request.d.ts.map +1 -0
- package/dist/fetch-request.js +69 -0
- package/dist/fetch-request.js.map +1 -0
- package/dist/fetch-response.d.ts +24 -0
- package/dist/fetch-response.d.ts.map +1 -0
- package/dist/fetch-response.js +151 -0
- package/dist/fetch-response.js.map +1 -0
- package/dist/fetch-wrap.d.ts +10 -0
- package/dist/fetch-wrap.d.ts.map +1 -0
- package/dist/fetch-wrap.js +73 -0
- package/dist/fetch-wrap.js.map +1 -0
- package/dist/fetch.d.ts +2 -0
- package/dist/fetch.d.ts.map +1 -0
- package/dist/fetch.js +3 -0
- package/dist/fetch.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/transformed-response.d.ts +12 -0
- package/dist/transformed-response.d.ts.map +1 -0
- package/dist/transformed-response.js +47 -0
- package/dist/transformed-response.js.map +1 -0
- package/dist/util.d.ts +24 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +51 -0
- package/dist/util.js.map +1 -0
- package/package.json +36 -0
- package/src/fetch-error.ts +77 -0
- package/src/fetch-request.ts +86 -0
- package/src/fetch-response.ts +212 -0
- package/src/fetch-wrap.ts +101 -0
- package/src/fetch.ts +4 -0
- package/src/index.ts +6 -0
- package/src/transformed-response.ts +33 -0
- package/src/util.ts +59 -0
- package/tsconfig.json +8 -0
package/src/util.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// TODO: Move to a shared package ?
|
|
2
|
+
|
|
3
|
+
export type JsonScalar = string | number | boolean | null
|
|
4
|
+
export type Json = JsonScalar | Json[] | { [key: string]: undefined | Json }
|
|
5
|
+
export type JsonObject = { [key: string]: Json }
|
|
6
|
+
export type JsonArray = Json[]
|
|
7
|
+
|
|
8
|
+
declare global {
|
|
9
|
+
interface JSON {
|
|
10
|
+
parse(text: string, reviver?: (key: any, value: any) => any): Json
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function isIp(hostname: string) {
|
|
15
|
+
// IPv4
|
|
16
|
+
if (hostname.match(/^\d+\.\d+\.\d+\.\d+$/)) return true
|
|
17
|
+
|
|
18
|
+
// IPv6
|
|
19
|
+
if (hostname.startsWith('[') && hostname.endsWith(']')) return true
|
|
20
|
+
|
|
21
|
+
return false
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// TODO: Move to a shared package ?
|
|
25
|
+
|
|
26
|
+
const plainObjectProto = Object.prototype
|
|
27
|
+
export const ifObject = <V>(v?: V) => {
|
|
28
|
+
if (typeof v === 'object' && v != null && !Array.isArray(v)) {
|
|
29
|
+
const proto = Object.getPrototypeOf(v)
|
|
30
|
+
if (proto === null || proto === plainObjectProto) {
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
32
|
+
return v as V extends JsonScalar | JsonArray | Function | symbol
|
|
33
|
+
? never
|
|
34
|
+
: V extends Json
|
|
35
|
+
? V
|
|
36
|
+
: // Plain object are (mostly) safe to access as Json
|
|
37
|
+
{ [key: string]: unknown }
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const ifArray = <V>(v?: V) => (Array.isArray(v) ? v : undefined)
|
|
45
|
+
export const ifScalar = <V>(v?: V) => {
|
|
46
|
+
switch (typeof v) {
|
|
47
|
+
case 'string':
|
|
48
|
+
case 'number':
|
|
49
|
+
case 'boolean':
|
|
50
|
+
return v
|
|
51
|
+
default:
|
|
52
|
+
if (v === null) return null as V & null
|
|
53
|
+
return undefined as V extends JsonScalar ? never : undefined
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export const ifBoolean = <V>(v?: V) => (typeof v === 'boolean' ? v : undefined)
|
|
57
|
+
export const ifString = <V>(v?: V) => (typeof v === 'string' ? v : undefined)
|
|
58
|
+
export const ifNumber = <V>(v?: V) => (typeof v === 'number' ? v : undefined)
|
|
59
|
+
export const ifNull = <V>(v?: V) => (v === null ? v : undefined)
|