-
-
Notifications
You must be signed in to change notification settings - Fork 257
/
UrlParams.ts
231 lines (213 loc) · 6 KB
/
UrlParams.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* @since 1.0.0
*/
import type { ParseOptions } from "@effect/schema/AST"
import type * as ParseResult from "@effect/schema/ParseResult"
import * as Schema from "@effect/schema/Schema"
import * as Arr from "effect/Array"
import type * as Effect from "effect/Effect"
import * as Either from "effect/Either"
import { dual } from "effect/Function"
import * as Option from "effect/Option"
/**
* @since 1.0.0
* @category models
*/
export interface UrlParams extends ReadonlyArray<readonly [string, string]> {}
/**
* @since 1.0.0
* @category models
*/
export type Input =
| Readonly<Record<string, Coercible | ReadonlyArray<Coercible>>>
| Iterable<readonly [string, Coercible]>
| URLSearchParams
/**
* @since 1.0.0
* @category models
*/
export type Coercible = string | number | bigint | boolean | null | undefined
/**
* @since 1.0.0
* @category constructors
*/
export const fromInput = (input: Input): UrlParams => {
const entries = Symbol.iterator in input ? Arr.fromIterable(input) : Object.entries(input)
const out: Array<readonly [string, string]> = []
for (const [key, value] of entries) {
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
if (value[i] !== undefined) {
out.push([key, String(value[i])])
}
}
} else if (value !== undefined) {
out.push([key, String(value)])
}
}
return out
}
/**
* @since 1.0.0
* @category schemas
*/
export const schema: Schema.Schema<UrlParams, ReadonlyArray<readonly [string, string]>> = Schema.Array(
Schema.Tuple(Schema.String, Schema.String)
).annotations({ identifier: "UrlParams" })
/**
* @since 1.0.0
* @category constructors
*/
export const empty: UrlParams = []
/**
* @since 1.0.0
* @category combinators
*/
export const getAll: {
(key: string): (self: UrlParams) => ReadonlyArray<string>
(self: UrlParams, key: string): ReadonlyArray<string>
} = dual(
2,
(self: UrlParams, key: string): ReadonlyArray<string> =>
Arr.reduce(self, [] as Array<string>, (acc, [k, value]) => {
if (k === key) {
acc.push(value)
}
return acc
})
)
/**
* @since 1.0.0
* @category combinators
*/
export const getFirst: {
(key: string): (self: UrlParams) => Option.Option<string>
(self: UrlParams, key: string): Option.Option<string>
} = dual(2, (self: UrlParams, key: string): Option.Option<string> =>
Option.map(
Arr.findFirst(self, ([k]) => k === key),
([, value]) => value
))
/**
* @since 1.0.0
* @category combinators
*/
export const getLast: {
(key: string): (self: UrlParams) => Option.Option<string>
(self: UrlParams, key: string): Option.Option<string>
} = dual(2, (self: UrlParams, key: string): Option.Option<string> =>
Option.map(
Arr.findLast(self, ([k]) => k === key),
([, value]) => value
))
/**
* @since 1.0.0
* @category combinators
*/
export const set: {
(key: string, value: Coercible): (self: UrlParams) => UrlParams
(self: UrlParams, key: string, value: Coercible): UrlParams
} = dual(3, (self: UrlParams, key: string, value: Coercible): UrlParams =>
Arr.append(
Arr.filter(self, ([k]) => k !== key),
[key, String(value)]
))
/**
* @since 1.0.0
* @category combinators
*/
export const setAll: {
(input: Input): (self: UrlParams) => UrlParams
(self: UrlParams, input: Input): UrlParams
} = dual(2, (self: UrlParams, input: Input): UrlParams => {
const toSet = fromInput(input)
const keys = toSet.map(([k]) => k)
return Arr.appendAll(
Arr.filter(self, ([k]) => keys.includes(k)),
toSet
)
})
/**
* @since 1.0.0
* @category combinators
*/
export const append: {
(key: string, value: Coercible): (self: UrlParams) => UrlParams
(self: UrlParams, key: string, value: Coercible): UrlParams
} = dual(3, (self: UrlParams, key: string, value: Coercible): UrlParams =>
Arr.append(
self,
[key, String(value)]
))
/**
* @since 1.0.0
* @category combinators
*/
export const appendAll: {
(input: Input): (self: UrlParams) => UrlParams
(self: UrlParams, input: Input): UrlParams
} = dual(2, (self: UrlParams, input: Input): UrlParams => Arr.appendAll(self, fromInput(input)))
/**
* @since 1.0.0
* @category combinators
*/
export const remove: {
(key: string): (self: UrlParams) => UrlParams
(self: UrlParams, key: string): UrlParams
} = dual(2, (self: UrlParams, key: string): UrlParams => Arr.filter(self, ([k]) => k !== key))
/**
* @since 1.0.0
* @category combinators
*/
export const toString = (self: UrlParams): string => new URLSearchParams(self as any).toString()
/**
* @since 1.0.0
* @category constructors
*/
export const makeUrl = (url: string, params: UrlParams, hash: Option.Option<string>): Either.Either<URL, Error> => {
try {
const urlInstance = new URL(url, baseUrl())
for (let i = 0; i < params.length; i++) {
const [key, value] = params[i]
if (value !== undefined) {
urlInstance.searchParams.append(key, value)
}
}
if (hash._tag === "Some") {
urlInstance.hash = hash.value
}
return Either.right(urlInstance)
} catch (e) {
return Either.left(e as Error)
}
}
const baseUrl = (): string | undefined => {
if (
"location" in globalThis &&
globalThis.location !== undefined &&
globalThis.location.origin !== undefined &&
globalThis.location.pathname !== undefined
) {
return location.origin + location.pathname
}
return undefined
}
/**
* @since 1.0.0
* @category schema
*/
export const schemaJson = <A, I, R>(schema: Schema.Schema<A, I, R>, options?: ParseOptions | undefined): {
(
field: string
): (self: UrlParams) => Effect.Effect<A, ParseResult.ParseError, R>
(
self: UrlParams,
field: string
): Effect.Effect<A, ParseResult.ParseError, R>
} => {
const parse = Schema.decodeUnknown(Schema.parseJson(schema), options)
return dual<
(field: string) => (self: UrlParams) => Effect.Effect<A, ParseResult.ParseError, R>,
(self: UrlParams, field: string) => Effect.Effect<A, ParseResult.ParseError, R>
>(2, (self, field) => parse(Option.getOrElse(getLast(self, field), () => "")))
}