fix export interface stuff

This commit is contained in:
Matt Hill
2023-05-03 16:59:05 -06:00
parent 0ddd35c8d7
commit 24dc9f0510
4 changed files with 45 additions and 51 deletions

View File

@@ -1,19 +1,26 @@
export class Origin {
constructor(readonly protocol: string | null, readonly host: string) {}
withAuth(
origin?:
| {
password: null | string
username: string
}
| null
| undefined,
) {
build({ basic, path, search }: BuildOptions) {
// prettier-ignore
const urlAuth = !!(origin) ? `${origin.username}${origin.password != null ?`:${origin.password}`:''}@` :
const urlAuth = !!(basic) ? `${basic.username}${basic.password != null ?`:${basic.password}`:''}@` :
'';
const protocolSection = this.protocol != null ? `${this.protocol}://` : ""
return `${protocolSection}${urlAuth}${this.host}`
const qpEntries = Object.entries(search)
.map(
([key, val]) => `${encodeURIComponent(key)}=${encodeURIComponent(val)}`,
)
.join("&")
const qp = qpEntries.length ? `?${qpEntries}` : ""
return `${protocolSection}${urlAuth}${this.host}${path}${qp}`
}
}
type BuildOptions = {
basic: { username: string; password: string | null } | null
path: string
search: Record<string, string>
}