@api-client/core 0.5.16 → 0.5.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -134,11 +134,14 @@ interface IPart {
134
134
  variables: IVariable[];
135
135
  }
136
136
 
137
- export interface IUriTemplateOptions {
137
+ export interface IUriTemplateParseOptions {
138
138
  /**
139
- * Throws when a variable is not found to replaces a value in the template.
139
+ * Throws an error when template literals are invalid
140
140
  */
141
141
  strict?: boolean;
142
+ }
143
+
144
+ export interface IUriTemplateOptions extends IUriTemplateParseOptions {
142
145
  /**
143
146
  * When set it ignores replacing the value in the template when the variable is missing.
144
147
  */
@@ -253,13 +256,15 @@ export class UriTemplate {
253
256
  expand(map: Record<string, any>, opts: IUriTemplateOptions = {}): string {
254
257
  let result = '';
255
258
  if (!this.parts || !this.parts.length) {
256
- this.parse();
259
+ this.parse(opts);
257
260
  }
258
- const data = new Data(map);
259
-
261
+ const data = UriTemplate.getData(map);
260
262
  for (const part of this.parts!) {
261
- const item = typeof part === 'string' ? part : UriTemplate.expand(part, data, opts);
262
- result += item;
263
+ if (typeof part === 'string') {
264
+ result += UriTemplate.expandString(part);
265
+ } else {
266
+ result += UriTemplate.expand(part, data, opts);
267
+ }
263
268
  }
264
269
  return result;
265
270
  }
@@ -267,13 +272,13 @@ export class UriTemplate {
267
272
  /**
268
273
  * Parses the template into action tokens.
269
274
  */
270
- parse(): void {
275
+ parse(opts: IUriTemplateParseOptions = {}): void {
271
276
  const { expression } = this;
272
277
  const parts: (string | IPart)[] = [];
273
278
  let pos = 0;
274
279
 
275
280
  function checkLiteral(literal: string): string {
276
- if (literal.match(LITERAL_PATTERN)) {
281
+ if (opts.strict && literal.match(LITERAL_PATTERN)) {
277
282
  throw new Error(`Invalid Literal "${literal}"`);
278
283
  }
279
284
  return literal;
@@ -338,12 +343,31 @@ export class UriTemplate {
338
343
  this.parts = parts;
339
344
  }
340
345
 
346
+ /**
347
+ * Creates a Data object with the passed map.
348
+ *
349
+ * @param map The map with values.
350
+ */
351
+ protected static getData(map: Record<string, any>): Data {
352
+ return new Data(map);
353
+ }
354
+
355
+ /**
356
+ * Can be used by a child class to manipulate string parts that are not template expressions
357
+ *
358
+ * @param part The string part
359
+ * @returns The processed part.
360
+ */
361
+ protected static expandString(part: string): string {
362
+ return part;
363
+ }
364
+
341
365
  protected static expand(expression: IPart, data: Data, opts: IUriTemplateOptions = {}): string {
342
366
  const options = operators[expression.operator];
343
367
  const type = options.named ? 'Named' : 'Unnamed';
344
368
  const { variables } = expression;
345
369
  const buffer = [];
346
-
370
+
347
371
  for (const variable of variables) {
348
372
  const d = data.get(variable.name);
349
373
  if (d.type === DataType.nil && opts && opts.strict) {
@@ -444,7 +468,6 @@ export class UriTemplate {
444
468
  let result = '';
445
469
  const { encode, empty_name_separator } = options;
446
470
  const _encode = !d[encode].length;
447
-
448
471
  d.val.forEach((item, index) => {
449
472
  let _value: string;
450
473
  if (length) {