oidc-client-rx
Version:
ReactiveX enhanced OIDC and OAuth2 protocol support for browser-based JavaScript applications
186 lines (185 loc) • 5.87 kB
JavaScript
/*! For license information please see params.js.LICENSE.txt */
import { HttpParams } from "@ngify/http";
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/ class HttpUrlEncodingCodec {
encodeKey(key) {
return standardEncoding(key);
}
encodeValue(value) {
return standardEncoding(value);
}
decodeKey(key) {
return decodeURIComponent(key);
}
decodeValue(value) {
return decodeURIComponent(value);
}
}
const STANDARD_ENCODING_REGEX = /%(\d[a-f0-9])/gi;
const STANDARD_ENCODING_REPLACEMENTS = {
40: "@",
"3A": ":",
24: "$",
"2C": ",",
"3B": ";",
"3D": "=",
"3F": "?",
"2F": "/"
};
function standardEncoding(v) {
return encodeURIComponent(v).replace(STANDARD_ENCODING_REGEX, (s, t)=>STANDARD_ENCODING_REPLACEMENTS[t] ?? s);
}
function paramParser(rawParams, codec) {
const map = new Map();
if (rawParams.length > 0) {
const params = rawParams.replace(/^\?/, "").split("&");
params.forEach((param)=>{
const eqIdx = param.indexOf("=");
const [key, val] = -1 === eqIdx ? [
codec.decodeKey(param),
""
] : [
codec.decodeKey(param.slice(0, eqIdx)),
codec.decodeValue(param.slice(eqIdx + 1))
];
const list = map.get(key) || [];
list.push(val);
map.set(key, list);
});
}
return map;
}
class params_HttpParams {
has(param) {
this.init();
return this.map.has(param);
}
get(param) {
this.init();
const res = this.map.get(param);
return res ? res[0] : null;
}
getAll(param) {
this.init();
return this.map.get(param) || null;
}
keys() {
this.init();
return Array.from(this.map.keys());
}
append(param, value) {
return this.clone({
param,
value,
op: "a"
});
}
appendAll(params) {
const updates = [];
Object.keys(params).forEach((param)=>{
const value = params[param];
if (Array.isArray(value)) value.forEach((_value)=>{
updates.push({
param,
value: _value,
op: "a"
});
});
else updates.push({
param,
value: value,
op: "a"
});
});
return this.clone(updates);
}
set(param, value) {
return this.clone({
param,
value,
op: "s"
});
}
delete(param, value) {
return this.clone({
param,
value,
op: "d"
});
}
toString() {
this.init();
return this.keys().map((key)=>{
const eKey = this.encoder.encodeKey(key);
return this.map.get(key).map((value)=>`${eKey}=${this.encoder.encodeValue(value)}`).join("&");
}).filter((param)=>"" !== param).join("&");
}
toNgify() {
this.init();
return new HttpParams().appendAll(Object.fromEntries(this.map.entries()));
}
clone(update) {
const clone = new params_HttpParams({
encoder: this.encoder
});
clone.cloneFrom = this.cloneFrom || this;
clone.updates = (this.updates || []).concat(update);
return clone;
}
init() {
if (null === this.map) this.map = new Map();
if (null !== this.cloneFrom) {
this.cloneFrom.init();
this.cloneFrom.keys().forEach((key)=>{
this.map.set(key, this.cloneFrom.map.get(key));
});
this.updates.forEach((update)=>{
switch(update.op){
case "a":
case "s":
{
const base = ("a" === update.op ? this.map.get(update.param) : void 0) || [];
base.push(`${update.value}`);
this.map.set(update.param, base);
break;
}
case "d":
if (void 0 !== update.value) {
const base = this.map.get(update.param) || [];
const idx = base.indexOf(`${update.value}`);
if (-1 !== idx) base.splice(idx, 1);
if (base.length > 0) this.map.set(update.param, base);
else this.map.delete(update.param);
} else this.map.delete(update.param);
break;
default:
}
});
this.cloneFrom = this.updates = null;
}
}
constructor(options = {}){
this.updates = null;
this.cloneFrom = null;
this.encoder = options.encoder || new HttpUrlEncodingCodec();
if (options.fromString) {
if (options.fromObject) throw new Error("Cannot specify both fromString and fromObject.");
this.map = paramParser(options.fromString, this.encoder);
} else if (options.fromObject) {
this.map = new Map();
Object.keys(options.fromObject).forEach((key)=>{
const value = options.fromObject[key];
const values = Array.isArray(value) ? value.map((value)=>`${value}`) : [
`${value}`
];
this.map.set(key, values);
});
} else this.map = null;
}
}
export { params_HttpParams as HttpParams, HttpUrlEncodingCodec };