Skip to content

Commit

Permalink
fix: javascript clients accepted context rather than options (#51)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: if you previously passed `context` directly as the final
  parameter to the `analytics.js` or `analytics-node` clients, then you'll
  need to update it, like so:

  If you made a call like:

  ```
  typewriter.thingHappened({ when: 'now' }, { groupId: 123 })
  ```

  Then you would need to update it to:

  ```
  typewriter.thingHappened({ when: 'now' }, {
    context: { groupId: 123 }
  })
  ```

  This allows you to pass `integrations` and other fields in through this
  object, and aligns the TypeScript declarations with the underlying library.
  • Loading branch information
colinking authored Jan 30, 2019
1 parent 5b77361 commit 5222145
Show file tree
Hide file tree
Showing 14 changed files with 505 additions and 216 deletions.
54 changes: 39 additions & 15 deletions examples/gen-js/js/analytics/generated/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
const genOptions = (context = {}) => ({
context: {
...context,
typewriter: {
name: "gen-js",
version: "5.1.8"
}
}
});
export default class Analytics {
/**
* Instantiate a wrapper around an analytics library instance
Expand All @@ -18,7 +9,16 @@ export default class Analytics {
}
this.analytics = analytics || { track: () => null };
}
feedViewed(props = {}, context) {
addTypewriterContext(context = {}) {
return {
...context,
typewriter: {
name: "gen-js",
version: "5.1.8"
}
};
}
feedViewed(props = {}, options = {}, callback) {
var validate = function(
data,
dataPath,
Expand Down Expand Up @@ -99,9 +99,17 @@ export default class Analytics {
if (!validate({ properties: props })) {
throw new Error(JSON.stringify(validate.errors, null, 2));
}
this.analytics.track("Feed Viewed", props, genOptions(context));
this.analytics.track(
"Feed Viewed",
props,
{
...options,
context: this.addTypewriterContext(options.context)
},
callback
);
}
photoViewed(props = {}, context) {
photoViewed(props = {}, options = {}, callback) {
var validate = function(
data,
dataPath,
Expand Down Expand Up @@ -182,9 +190,17 @@ export default class Analytics {
if (!validate({ properties: props })) {
throw new Error(JSON.stringify(validate.errors, null, 2));
}
this.analytics.track("Photo Viewed", props, genOptions(context));
this.analytics.track(
"Photo Viewed",
props,
{
...options,
context: this.addTypewriterContext(options.context)
},
callback
);
}
profileViewed(props = {}, context) {
profileViewed(props = {}, options = {}, callback) {
var validate = function(
data,
dataPath,
Expand Down Expand Up @@ -265,6 +281,14 @@ export default class Analytics {
if (!validate({ properties: props })) {
throw new Error(JSON.stringify(validate.errors, null, 2));
}
this.analytics.track("Profile Viewed", props, genOptions(context));
this.analytics.track(
"Profile Viewed",
props,
{
...options,
context: this.addTypewriterContext(options.context)
},
callback
);
}
}
19 changes: 10 additions & 9 deletions examples/gen-js/node/analytics/generated/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const genOptions = (context = {}) => ({
context: Object.assign({}, context, {
typewriter: {
name: "gen-js",
version: "5.1.8"
}
})
});
class Analytics {
/**
* Instantiate a wrapper around an analytics library instance
Expand All @@ -19,6 +11,14 @@ class Analytics {
}
this.analytics = analytics || { track: () => null };
}
addTypewriterContext(context = {}) {
return Object.assign({}, context, {
typewriter: {
name: "gen-js",
version: "5.1.8"
}
});
}
orderCompleted(message = {}, callback) {
var validate = function(
data,
Expand Down Expand Up @@ -565,7 +565,8 @@ class Analytics {
if (!validate(message)) {
throw new Error(JSON.stringify(validate.errors, null, 2));
}
message = Object.assign({}, message, genOptions(message.context), {
message = Object.assign({}, message, {
context: this.addTypewriterContext(message.context),
event: "Order Completed"
});
this.analytics.track(message, callback);
Expand Down
54 changes: 39 additions & 15 deletions examples/gen-js/ts/analytics/generated/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
const genOptions = (context = {}) => ({
context: {
...context,
typewriter: {
name: "gen-js",
version: "5.1.8"
}
}
});
export default class Analytics {
/**
* Instantiate a wrapper around an analytics library instance
Expand All @@ -18,7 +9,16 @@ export default class Analytics {
}
this.analytics = analytics || { track: () => null };
}
feedViewed(props = {}, context) {
addTypewriterContext(context = {}) {
return {
...context,
typewriter: {
name: "gen-js",
version: "5.1.8"
}
};
}
feedViewed(props = {}, options = {}, callback) {
var validate = function(
data,
dataPath,
Expand Down Expand Up @@ -99,9 +99,17 @@ export default class Analytics {
if (!validate({ properties: props })) {
throw new Error(JSON.stringify(validate.errors, null, 2));
}
this.analytics.track("Feed Viewed", props, genOptions(context));
this.analytics.track(
"Feed Viewed",
props,
{
...options,
context: this.addTypewriterContext(options.context)
},
callback
);
}
photoViewed(props = {}, context) {
photoViewed(props = {}, options = {}, callback) {
var validate = function(
data,
dataPath,
Expand Down Expand Up @@ -182,9 +190,17 @@ export default class Analytics {
if (!validate({ properties: props })) {
throw new Error(JSON.stringify(validate.errors, null, 2));
}
this.analytics.track("Photo Viewed", props, genOptions(context));
this.analytics.track(
"Photo Viewed",
props,
{
...options,
context: this.addTypewriterContext(options.context)
},
callback
);
}
profileViewed(props = {}, context) {
profileViewed(props = {}, options = {}, callback) {
var validate = function(
data,
dataPath,
Expand Down Expand Up @@ -265,6 +281,14 @@ export default class Analytics {
if (!validate({ properties: props })) {
throw new Error(JSON.stringify(validate.errors, null, 2));
}
this.analytics.track("Profile Viewed", props, genOptions(context));
this.analytics.track(
"Profile Viewed",
props,
{
...options,
context: this.addTypewriterContext(options.context)
},
callback
);
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"generate-examples:js": "cd ./examples/gen-js/js && yarn run typewriter",
"generate-examples:ts": "cd ./examples/gen-js/ts && yarn run typewriter",
"generate-examples:node": "cd ./examples/gen-js/node && yarn run typewriter",
"generate-examples": "run-p generate-examples:*"
"generate-examples": "yarn run build && run-p generate-examples:*"
},
"dependencies": {
"@nucleartide/dx": "^1.0.0",
Expand Down
29 changes: 16 additions & 13 deletions src/commands/gen-js/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ export function genJS(
throw new Error('An instance of ${clientName} must be provided')
}`
const fileHeader = `
const genOptions = (context = {}) => ({
context: {
...context,
typewriter: {
name: "${command}",
version: "${version}"
}
}
})
export default class Analytics {
/**
* Instantiate a wrapper around an analytics library instance
Expand All @@ -48,6 +38,16 @@ export function genJS(
${runtimeValidation ? analyticsValidation : ''}
this.analytics = analytics || { track: () => null }
}
addTypewriterContext(context = {}) {
return {
...context,
typewriter: {
name: "${command}",
version: "${version}"
}
}
}
`

const trackCalls =
Expand All @@ -61,15 +61,18 @@ export function genJS(
let trackCall = ''
let validateCall = ''
if (client === Client.js) {
parameters = 'props = {}, context'
trackCall = `this.analytics.track('${name}', props, genOptions(context))`
parameters = 'props = {}, options = {}, callback'
trackCall = `this.analytics.track('${name}', props, {
...options,
context: this.addTypewriterContext(options.context)
}, callback)`
validateCall = 'validate({ properties: props })'
} else if (client === Client.node) {
parameters = 'message = {}, callback'
trackCall = `
message = {
...message,
...genOptions(message.context),
context: this.addTypewriterContext(message.context),
event: '${name}'
}
this.analytics.track(message, callback)`
Expand Down
Loading

0 comments on commit 5222145

Please sign in to comment.