Skip to content

Commit

Permalink
refactor: move updateInteractionNameWithUriVariablePattern to core
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Sep 11, 2023
1 parent c8177c2 commit a6d62fa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
40 changes: 3 additions & 37 deletions packages/binding-http/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,40 +261,6 @@ export default class HttpServer implements ProtocolServer {
}
}

private updateInteractionNameWithUriVariablePattern(
interactionName: string,
uriVariables: PropertyElement["uriVariables"] = {},
thingVariables: PropertyElement["uriVariables"] = {}
): string {
const variables = Object.assign({}, uriVariables, thingVariables);
if (Object.keys(variables).length > 0) {
let pattern = "{?";
let index = 0;
if (uriVariables) {
for (const key in uriVariables) {
if (index !== 0) {
pattern += ",";
}
pattern += encodeURIComponent(key);
index++;
}
}
if (thingVariables) {
for (const key in thingVariables) {
if (index !== 0) {
pattern += ",";
}
pattern += encodeURIComponent(key);
index++;
}
}
pattern += "}";
return encodeURIComponent(interactionName) + pattern;
} else {
return encodeURIComponent(interactionName);
}
}

public async expose(thing: ExposedThing, tdTemplate: WoT.ExposedThingInit = {}): Promise<void> {
let urlPath = slugify(thing.title, { lower: true });

Expand Down Expand Up @@ -400,7 +366,7 @@ export default class HttpServer implements ProtocolServer {
}

for (const propertyName in thing.properties) {
const propertyNamePattern = this.updateInteractionNameWithUriVariablePattern(
const propertyNamePattern = Helpers.updateInteractionNameWithUriVariablePattern(
propertyName,
thing.properties[propertyName].uriVariables,
thing.uriVariables
Expand Down Expand Up @@ -453,7 +419,7 @@ export default class HttpServer implements ProtocolServer {
}

for (const actionName in thing.actions) {
const actionNamePattern = this.updateInteractionNameWithUriVariablePattern(
const actionNamePattern = Helpers.updateInteractionNameWithUriVariablePattern(
actionName,
thing.actions[actionName].uriVariables,
thing.uriVariables
Expand All @@ -475,7 +441,7 @@ export default class HttpServer implements ProtocolServer {
}

for (const eventName in thing.events) {
const eventNamePattern = this.updateInteractionNameWithUriVariablePattern(
const eventNamePattern = Helpers.updateInteractionNameWithUriVariablePattern(
eventName,
thing.events[eventName].uriVariables,
thing.uriVariables
Expand Down
36 changes: 35 additions & 1 deletion packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { DataSchemaValue, ExposedThingInit } from "wot-typescript-definitions";
import { SomeJSONSchema } from "ajv/dist/types/json-schema";
import { ThingInteraction, ThingModelHelpers } from "@node-wot/td-tools";
import { Resolver } from "@node-wot/td-tools/src/resolver-interface";
import { DataSchema } from "wot-thing-description-types";
import { PropertyElement, DataSchema } from "wot-thing-description-types";
import { createLoggers } from "./logger";

const { debug, error, warn } = createLoggers("core", "helpers");
Expand Down Expand Up @@ -386,4 +386,38 @@ export default class Helpers implements Resolver {

return params;
}

public static updateInteractionNameWithUriVariablePattern(
interactionName: string,
uriVariables: PropertyElement["uriVariables"] = {},
thingVariables: PropertyElement["uriVariables"] = {}
): string {
const variables = Object.assign({}, uriVariables, thingVariables);
if (Object.keys(variables).length > 0) {
let pattern = "{?";
let index = 0;
if (uriVariables) {
for (const key in uriVariables) {
if (index !== 0) {
pattern += ",";
}
pattern += encodeURIComponent(key);
index++;
}
}
if (thingVariables) {
for (const key in thingVariables) {
if (index !== 0) {
pattern += ",";
}
pattern += encodeURIComponent(key);
index++;
}
}
pattern += "}";
return encodeURIComponent(interactionName) + pattern;
} else {
return encodeURIComponent(interactionName);
}
}
}

0 comments on commit a6d62fa

Please sign in to comment.