Files
start-sdk/lib/mainFn/Origin.ts
2023-05-04 16:42:42 -06:00

27 lines
711 B
TypeScript

export class Origin {
constructor(readonly protocol: string | null, readonly host: string) {}
build({ username, path, search }: BuildOptions) {
// prettier-ignore
const urlAuth = !!(username) ? `${username}@` :
'';
const protocolSection = this.protocol != null ? `${this.protocol}://` : ""
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 = {
username: string | null
path: string
search: Record<string, string>
}