Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3 analytic funnels #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions js/tweek-rest/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// <reference path="./node_modules/@types/isomorphic-fetch/index.d.ts"/>

import * as queryString from 'query-string';

declare function fetch(x: any): Promise<{ json: <T>() => Promise<T> }>

export type IdentityContext = { id?: string; } & {
[prop: string]: string;
}
Expand All @@ -22,6 +24,7 @@ export type FetchConfig = {
export type TweekInitConfig = FetchConfig & {
baseServiceUrl: string;
restGetter: <T>(url: string) => Promise<T>;
restPoster: <T>(url: string) => Promise<T>;
}

function captialize(string) {
Expand Down Expand Up @@ -67,15 +70,26 @@ export class TweekClient implements ITweekClient {
constructor(config: TweekInitConfig) {
this.config = <TweekInitConfig & FetchConfig>
{ ...{ 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<any> {
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<void>(url);
}

fetch<T>(path: string, _config?: FetchConfig): Promise<T> {
const { casing, flatten, baseServiceUrl, restGetter, convertTyping, context, include } =
<TweekInitConfig & FetchConfig>{ ...this.config, ..._config };
Expand All @@ -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<any>(url);

if (!flatten && casing === "camelCase") {
Expand Down Expand Up @@ -119,12 +133,14 @@ export class TweekClient implements ITweekClient {

export function createTweekClient(baseServiceUrl: string,
context: any,
restGetter: <T>(url: string) => Promise<T> = <T>(url: string) => fetch(url).then(r => r.json<T>())) {
restGetter: <T>(url: string) => Promise<T> = <T>(url: string) => fetch(url).then(r => r.json<T>()),
restPoster: <T>(url: string) => Promise<T> = <T>(url: string) => fetch({ url, params: { method: 'POST' } })) {
return new TweekClient({
baseServiceUrl,
casing: "camelCase",
convertTyping: true,
context,
restGetter
restGetter,
restPoster
});
}
1 change: 0 additions & 1 deletion js/tweek-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion js/tweek-rest/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down