Skip to content

Commit

Permalink
fix: optional message parameter in JS clients (#47)
Browse files Browse the repository at this point in the history
This makes `message` an optional parameter for auto-generated `track` calls, allowing you to make calls in `ts`-compiled files without passing in a message:

```
typewriter.fooBar()
```

instead of:

```
typewriter.fooBar({})
```

----

This also adds tests for ES5-transpiled clients (in this case, to validate that optional parameters were transpiled).
  • Loading branch information
colinking authored Jan 28, 2019
1 parent a3825a7 commit 177069d
Show file tree
Hide file tree
Showing 20 changed files with 2,386 additions and 63 deletions.
6 changes: 3 additions & 3 deletions examples/gen-js/js/analytics/generated/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ export default class Analytics {
constructor(analytics: any);

feedViewed(
message: FeedViewed,
message?: FeedViewed,
options?: SegmentOptions,
callback?: AnalyticsJSCallback
): void;

photoViewed(
message: PhotoViewed,
message?: PhotoViewed,
options?: SegmentOptions,
callback?: AnalyticsJSCallback
): void;

profileViewed(
message: ProfileViewed,
message?: ProfileViewed,
options?: SegmentOptions,
callback?: AnalyticsJSCallback
): void;
Expand Down
6 changes: 3 additions & 3 deletions examples/gen-js/js/analytics/generated/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class Analytics {
}
this.analytics = analytics || { track: () => null };
}
feedViewed(props, context) {
feedViewed(props = {}, context) {
var validate = function(
data,
dataPath,
Expand Down Expand Up @@ -101,7 +101,7 @@ export default class Analytics {
}
this.analytics.track("Feed Viewed", props, genOptions(context));
}
photoViewed(props, context) {
photoViewed(props = {}, context) {
var validate = function(
data,
dataPath,
Expand Down Expand Up @@ -184,7 +184,7 @@ export default class Analytics {
}
this.analytics.track("Photo Viewed", props, genOptions(context));
}
profileViewed(props, context) {
profileViewed(props = {}, context) {
var validate = function(
data,
dataPath,
Expand Down
2 changes: 1 addition & 1 deletion examples/gen-js/node/analytics/generated/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default class Analytics {
constructor(analytics: any);

orderCompleted(
message: TrackMessage<OrderCompleted>,
message?: TrackMessage<OrderCompleted>,
callback?: AnalyticsNodeCallback
): void;
}
2 changes: 1 addition & 1 deletion examples/gen-js/node/analytics/generated/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Analytics {
}
this.analytics = analytics || { track: () => null };
}
orderCompleted(message, callback) {
orderCompleted(message = {}, callback) {
var validate = function(
data,
dataPath,
Expand Down
6 changes: 3 additions & 3 deletions examples/gen-js/ts/analytics/generated/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ export default class Analytics {
constructor(analytics: any);

feedViewed(
message: FeedViewed,
message?: FeedViewed,
options?: SegmentOptions,
callback?: AnalyticsJSCallback
): void;

photoViewed(
message: PhotoViewed,
message?: PhotoViewed,
options?: SegmentOptions,
callback?: AnalyticsJSCallback
): void;

profileViewed(
message: ProfileViewed,
message?: ProfileViewed,
options?: SegmentOptions,
callback?: AnalyticsJSCallback
): void;
Expand Down
6 changes: 3 additions & 3 deletions examples/gen-js/ts/analytics/generated/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class Analytics {
}
this.analytics = analytics || { track: () => null };
}
feedViewed(props, context) {
feedViewed(props = {}, context) {
var validate = function(
data,
dataPath,
Expand Down Expand Up @@ -101,7 +101,7 @@ export default class Analytics {
}
this.analytics.track("Feed Viewed", props, genOptions(context));
}
photoViewed(props, context) {
photoViewed(props = {}, context) {
var validate = function(
data,
dataPath,
Expand Down Expand Up @@ -184,7 +184,7 @@ export default class Analytics {
}
this.analytics.track("Photo Viewed", props, genOptions(context));
}
profileViewed(props, context) {
profileViewed(props = {}, context) {
var validate = function(
data,
dataPath,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/gen-js/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ export function genJS(
let trackCall = ''
let validateCall = ''
if (client === Client.js) {
parameters = 'props, context'
parameters = 'props = {}, context'
trackCall = `this.analytics.track('${name}', props, genOptions(context))`
validateCall = 'validate({ properties: props })'
} else if (client === Client.node) {
parameters = 'message, callback'
parameters = 'message = {}, callback'
trackCall = `
message = {
...message,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/gen-js/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,14 @@ class AJSTSDeclarationsRenderer extends TypeScriptRenderer {
if (this.ajsOptions.client === Client.js) {
this.emitLine([
camelCaseName,
'(message: ',
'(props?: ',
this.sourceFor(t).source,
', options?: SegmentOptions, callback?: AnalyticsJSCallback): void'
])
} else if (this.ajsOptions.client === Client.node) {
this.emitLine([
camelCaseName,
'(message: TrackMessage<',
'(message?: TrackMessage<',
this.sourceFor(t).source,
'>, callback?: AnalyticsNodeCallback): void'
])
Expand Down
10 changes: 5 additions & 5 deletions tests/commands/gen-js/__snapshots__/index.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ define(["require", "exports"], function(require, exports) {
}
this.analytics = analytics || { track: () => null };
}
terribleEventName3(props, context) {
terribleEventName3(props = {}, context) {
var validate = function(
data,
dataPath,
Expand Down Expand Up @@ -79,7 +79,7 @@ define(["require", "exports"], function(require, exports) {
genOptions(context)
);
}
emptyEvent(props, context) {
emptyEvent(props = {}, context) {
var validate = function(
data,
dataPath,
Expand Down Expand Up @@ -142,7 +142,7 @@ define(["require", "exports"], function(require, exports) {
}
this.analytics.track("Empty Event", props, genOptions(context));
}
exampleEvent(props, context) {
exampleEvent(props = {}, context) {
var pattern0 = new RegExp("FOO|BAR");
var validate = function(
data,
Expand Down Expand Up @@ -1020,7 +1020,7 @@ define(["require", "exports"], function(require, exports) {
}
this.analytics.track("Example Event", props, genOptions(context));
}
draft04Event(props, context) {
draft04Event(props = {}, context) {
var validate = function(
data,
dataPath,
Expand Down Expand Up @@ -1083,7 +1083,7 @@ define(["require", "exports"], function(require, exports) {
}
this.analytics.track("Draft-04 Event", props, genOptions(context));
}
draft06Event(props, context) {
draft06Event(props = {}, context) {
var validate = function(
data,
dataPath,
Expand Down
10 changes: 5 additions & 5 deletions tests/commands/gen-js/__snapshots__/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export default class Analytics {
constructor(analytics: any);

the42TerribleEventName3(
message: The42_TerribleEventName3,
props?: The42_TerribleEventName3,
options?: SegmentOptions,
callback?: AnalyticsJSCallback
): void;
Expand All @@ -178,13 +178,13 @@ export default class Analytics {
* Required object (empty) property
*/
emptyEvent(
message: { [key: string]: any },
props?: { [key: string]: any },
options?: SegmentOptions,
callback?: AnalyticsJSCallback
): void;

exampleEvent(
message: ExampleEvent,
props?: ExampleEvent,
options?: SegmentOptions,
callback?: AnalyticsJSCallback
): void;
Expand All @@ -195,7 +195,7 @@ export default class Analytics {
* Required object (empty) property
*/
draft04Event(
message: { [key: string]: any },
props?: { [key: string]: any },
options?: SegmentOptions,
callback?: AnalyticsJSCallback
): void;
Expand All @@ -206,7 +206,7 @@ export default class Analytics {
* Required object (empty) property
*/
draft06Event(
message: { [key: string]: any },
props?: { [key: string]: any },
options?: SegmentOptions,
callback?: AnalyticsJSCallback
): void;
Expand Down
Loading

0 comments on commit 177069d

Please sign in to comment.