MCPcopy Create free account
hub / github.com/angular/angular / HttpParams

Class HttpParams

packages/common/http/src/params.ts:149–347  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

147 * @publicApi
148 */
149export class HttpParams {
150 private map: Map<string, string[]> | null;
151 private encoder: HttpParameterCodec;
152 private updates: Update[] | null = null;
153 private cloneFrom: HttpParams | null = null;
154
155 constructor(options: HttpParamsOptions = {} as HttpParamsOptions) {
156 this.encoder = options.encoder || new HttpUrlEncodingCodec();
157 if (options.fromString) {
158 if (options.fromObject) {
159 throw new RuntimeError(
160 RuntimeErrorCode.CANNOT_SPECIFY_BOTH_FROM_STRING_AND_FROM_OBJECT,
161 ngDevMode && 'Cannot specify both fromString and fromObject.',
162 );
163 }
164 this.map = paramParser(options.fromString, this.encoder);
165 } else if (!!options.fromObject) {
166 this.map = new Map<string, string[]>();
167 Object.keys(options.fromObject).forEach((key) => {
168 const value = (options.fromObject as any)[key];
169 // convert the values to strings
170 const values = Array.isArray(value) ? value.map(valueToString) : [valueToString(value)];
171 if (values.length > 0) {
172 this.map!.set(key, values);
173 }
174 });
175 } else {
176 this.map = null;
177 }
178 }
179
180 /**
181 * Reports whether the body includes one or more values for a given parameter.
182 * @param param The parameter name.
183 * @returns True if the parameter has one or more values,
184 * false if it has no value or is not present.
185 */
186 has(param: string): boolean {
187 this.init();
188 return this.map!.has(param);
189 }
190
191 /**
192 * Retrieves the first value for a parameter.
193 * @param param The parameter name.
194 * @returns The first value of the given parameter,
195 * or `null` if the parameter is not present.
196 */
197 get(param: string): string | null {
198 this.init();
199 const res = this.map!.get(param);
200 return !!res ? res[0] : null;
201 }
202
203 /**
204 * Retrieves all values for a parameter.
205 * @param param The parameter name.
206 * @returns All values in a string array,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected