diff --git a/js/tweek-rest/index.ts b/js/tweek-rest/index.ts
index 5fef20d5..5467b00f 100644
--- a/js/tweek-rest/index.ts
+++ b/js/tweek-rest/index.ts
@@ -1,6 +1,8 @@
-///
+
import * as queryString from 'query-string';
+declare function fetch(x: any): Promise<{ json: () => Promise }>
+
export type IdentityContext = { id?: string; } & {
[prop: string]: string;
}
@@ -22,6 +24,7 @@ export type FetchConfig = {
export type TweekInitConfig = FetchConfig & {
baseServiceUrl: string;
restGetter: (url: string) => Promise;
+ restPoster: (url: string) => Promise;
}
function captialize(string) {
@@ -67,15 +70,26 @@ export class TweekClient implements ITweekClient {
constructor(config: TweekInitConfig) {
this.config =
{ ...{ camelCase: "snake", flatten: false, convertTyping: false, context: {} }, ...config };
-
- let {baseServiceUrl} = config;
+
+ let { baseServiceUrl } = config;
if (baseServiceUrl.endsWith('/')) {
- baseServiceUrl = baseServiceUrl.substr(0, baseServiceUrl.length -1);
+ baseServiceUrl = baseServiceUrl.substr(0, baseServiceUrl.length - 1);
this.config.baseServiceUrl = baseServiceUrl;
}
}
+ dispatch(path: string, event: string): Promise {
+ if (!event) {
+ throw 'Argument "event" must be set';
+ }
+ const queryParamsObject = this._contextToQueryParams(this.config.context);
+ queryParamsObject['event'] = event;
+ const queryParams = queryString.stringify(queryParamsObject);
+ const url = [...this.config.baseServiceUrl.split("/"), 'funnel', path.split("/")].join("/") + (!!queryParams ? `?${queryParams}` : '');
+ return this.config.restPoster(url);
+ }
+
fetch(path: string, _config?: FetchConfig): Promise {
const { casing, flatten, baseServiceUrl, restGetter, convertTyping, context, include } =
{ ...this.config, ..._config };
@@ -89,7 +103,7 @@ export class TweekClient implements ITweekClient {
let queryParams = queryString.stringify(queryParamsObject);
queryParams = this.queryParamsEncoder(queryParams);
- const url = baseServiceUrl + (path.startsWith('/') ? '' : '/') + path + (!!queryParams ? `?${queryParams}` : '');
+ const url = [...this.config.baseServiceUrl.split("/"), 'keys', path.split("/")].join("/") + (!!queryParams ? `?${queryParams}` : '');
let result = restGetter(url);
if (!flatten && casing === "camelCase") {
@@ -119,12 +133,14 @@ export class TweekClient implements ITweekClient {
export function createTweekClient(baseServiceUrl: string,
context: any,
- restGetter: (url: string) => Promise = (url: string) => fetch(url).then(r => r.json())) {
+ restGetter: (url: string) => Promise = (url: string) => fetch(url).then(r => r.json()),
+ restPoster: (url: string) => Promise = (url: string) => fetch({ url, params: { method: 'POST' } })) {
return new TweekClient({
baseServiceUrl,
casing: "camelCase",
convertTyping: true,
context,
- restGetter
+ restGetter,
+ restPoster
});
}
diff --git a/js/tweek-rest/package.json b/js/tweek-rest/package.json
index 3b989912..6d080915 100644
--- a/js/tweek-rest/package.json
+++ b/js/tweek-rest/package.json
@@ -8,7 +8,6 @@
},
"devDependencies": {
"@types/chai": "^3.4.34",
- "@types/isomorphic-fetch": "0.0.31",
"@types/mocha": "^2.2.36",
"@types/sinon": "^1.16.34",
"@types/sinon-chai": "^2.7.27",
diff --git a/js/tweek-rest/test/index.spec.ts b/js/tweek-rest/test/index.spec.ts
index d6fa1ed3..3d6671e3 100644
--- a/js/tweek-rest/test/index.spec.ts
+++ b/js/tweek-rest/test/index.spec.ts
@@ -12,12 +12,14 @@ describe("tweek rest", () => {
const defaultUrl = 'http://test/';
let prepare = (url) => {
const restGetterStub = sinon.stub();
+ const restPosterStub = sinon.stub();
const tweekClient = new TweekClient({
baseServiceUrl: url || defaultUrl,
casing: "snake",
convertTyping: false,
- restGetter: restGetterStub
+ restGetter: restGetterStub,
+ restPoster: restPosterStub
});
return {