@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.
Files changed (44) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/LICENSE.txt +7 -0
  3. package/dist/fetch-error.d.ts +16 -0
  4. package/dist/fetch-error.d.ts.map +1 -0
  5. package/dist/fetch-error.js +73 -0
  6. package/dist/fetch-error.js.map +1 -0
  7. package/dist/fetch-request.d.ts +7 -0
  8. package/dist/fetch-request.d.ts.map +1 -0
  9. package/dist/fetch-request.js +69 -0
  10. package/dist/fetch-request.js.map +1 -0
  11. package/dist/fetch-response.d.ts +24 -0
  12. package/dist/fetch-response.d.ts.map +1 -0
  13. package/dist/fetch-response.js +151 -0
  14. package/dist/fetch-response.js.map +1 -0
  15. package/dist/fetch-wrap.d.ts +10 -0
  16. package/dist/fetch-wrap.d.ts.map +1 -0
  17. package/dist/fetch-wrap.js +73 -0
  18. package/dist/fetch-wrap.js.map +1 -0
  19. package/dist/fetch.d.ts +2 -0
  20. package/dist/fetch.d.ts.map +1 -0
  21. package/dist/fetch.js +3 -0
  22. package/dist/fetch.js.map +1 -0
  23. package/dist/index.d.ts +7 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +23 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/transformed-response.d.ts +12 -0
  28. package/dist/transformed-response.d.ts.map +1 -0
  29. package/dist/transformed-response.js +47 -0
  30. package/dist/transformed-response.js.map +1 -0
  31. package/dist/util.d.ts +24 -0
  32. package/dist/util.d.ts.map +1 -0
  33. package/dist/util.js +51 -0
  34. package/dist/util.js.map +1 -0
  35. package/package.json +36 -0
  36. package/src/fetch-error.ts +77 -0
  37. package/src/fetch-request.ts +86 -0
  38. package/src/fetch-response.ts +212 -0
  39. package/src/fetch-wrap.ts +101 -0
  40. package/src/fetch.ts +4 -0
  41. package/src/index.ts +6 -0
  42. package/src/transformed-response.ts +33 -0
  43. package/src/util.ts +59 -0
  44. 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)
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig/isomorphic.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "rootDir": "src"
6
+ },
7
+ "include": ["src"]
8
+ }