@outfoxx/sunday 2.0.0-beta.6 → 2.0.0-beta.7

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.
package/README.md CHANGED
@@ -89,3 +89,11 @@ values again. Custom transports must invoke the callback at the same boundary be
89
89
 
90
90
  Parameter failures throw `RequestValidationError` with the native validation error as `cause`.
91
91
  Event sources close on this error, and event stream iterators reject instead of reconnecting.
92
+
93
+ ## URI template variables
94
+
95
+ `URLTemplate.complete` preserves the distinction between an empty string and a missing,
96
+ `undefined`, or `null` value. For `/items{/id}`, `id: ''` produces `/items/`, while an
97
+ undefined `id` produces `/items`. An empty collection is undefined; a list containing
98
+ an empty string has a defined member. Per-call values override stored template parameters,
99
+ including explicit null or undefined values that remove a stored value for that expansion.
@@ -1,6 +1,9 @@
1
+ /** An RFC 6570 URL template with reusable parameter defaults. */
1
2
  export declare class URLTemplate {
2
3
  template: string;
3
4
  parameters: Record<string, unknown>;
5
+ /** Creates a template whose stored parameters can be overridden for each expansion. */
4
6
  constructor(template: string, parameters?: Record<string, unknown>);
7
+ /** Expands base and relative templates, preserving empty values and omitting null or missing values. */
5
8
  complete(relativeTemplate: string, parameters: Record<string, unknown>): URL;
6
9
  }
@@ -11,14 +11,17 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- import { parse } from 'uri-template';
14
+ import { expandExpression, parse } from 'uri-template';
15
+ /** An RFC 6570 URL template with reusable parameter defaults. */
15
16
  export class URLTemplate {
16
17
  template;
17
18
  parameters;
19
+ /** Creates a template whose stored parameters can be overridden for each expansion. */
18
20
  constructor(template, parameters = {}) {
19
21
  this.template = template;
20
22
  this.parameters = parameters;
21
23
  }
24
+ /** Expands base and relative templates, preserving empty values and omitting null or missing values. */
22
25
  complete(relativeTemplate, parameters) {
23
26
  const allParameters = { ...this.parameters, ...parameters };
24
27
  const baseTempl = this.template.endsWith('/')
@@ -27,7 +30,32 @@ export class URLTemplate {
27
30
  const relTempl = relativeTemplate.startsWith('/') || !relativeTemplate.length
28
31
  ? relativeTemplate
29
32
  : `/${relativeTemplate}`;
30
- return new URL(parse(baseTempl + relTempl).expand(allParameters));
33
+ const template = parse(baseTempl + relTempl);
34
+ const expanded = template.ast.parts
35
+ .map((part) => {
36
+ if (part.type === 'literal')
37
+ return part.value;
38
+ const value = expandExpression(part, allParameters);
39
+ // uri-template 2 drops the path prefix when defined variables expand to an empty string.
40
+ if (part.operator === '/' &&
41
+ value === '' &&
42
+ part.variables.some((variable) => isDefined(allParameters[variable.name]))) {
43
+ return '/';
44
+ }
45
+ return value;
46
+ })
47
+ .join('');
48
+ return new URL(expanded);
31
49
  }
32
50
  }
51
+ // RFC 6570 treats empty collections as undefined, but an empty string is defined.
52
+ function isDefined(value) {
53
+ if (value == null)
54
+ return false;
55
+ if (typeof value !== 'object')
56
+ return true;
57
+ if (Array.isArray(value))
58
+ return value.length > 0;
59
+ return Object.values(value).some((entry) => entry != null);
60
+ }
33
61
  //# sourceMappingURL=url-template.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"url-template.js","sourceRoot":"","sources":["../src/url-template.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAC9B,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,gDAAgD;AAChD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAErC,MAAM,OAAO,WAAW;IAEb;IACA;IAFT,YACS,QAAgB,EAChB,aAAsC,EAAE;QADxC,aAAQ,GAAR,QAAQ,CAAQ;QAChB,eAAU,GAAV,UAAU,CAA8B;IAC9C,CAAC;IAEJ,QAAQ,CAAC,gBAAwB,EAAE,UAAmC;QACpE,MAAM,aAAa,GAAG,EAAE,GAAG,IAAI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C,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;IAClD,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC;AAC7D,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@outfoxx/sunday",
3
- "version": "v2.0.0-beta.6",
3
+ "version": "v2.0.0-beta.7",
4
4
  "description": "Sunday 🙏 The framework of REST for TypeScript/JavaScript",
5
5
  "packageManager": "bun@1.3.10",
6
6
  "type": "module",