Skip to content

Commit

Permalink
Hard code package version (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-engel authored Apr 26, 2022
1 parent 1d5971d commit 730c64d
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 150 deletions.
5 changes: 5 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

ts-node ./check-version.ts
pretty-quick --staged
7 changes: 7 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.github/
.nyc_output/
dist/

.gitpod.yml
package.json
package-lock.json
37 changes: 33 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Changelog

All notable changes to this project will be documented in this file.

## [3.2.0]
Expand Down Expand Up @@ -42,81 +43,105 @@ All notable changes to this project will be documented in this file.
## [3.0.0]

- (**BREAKING**) Remove the dependency on `request` ([#62](https://github.com/customerio/customerio-node/pull/62))

- We don't expect this to break many consumers of `customerio-node`. Unless you were using [`request`](https://github.com/request/request#requestoptions-callback) specific options, you don't need to make any changes.

- (**BREAKING**) Return an `Error` instance for non-`2XX` status codes ([#62](https://github.com/customerio/customerio-node/pull/62))

- We don't expect this to break many consumers of `customerio-node`. Unless you were using `instanceof` to check the type of error returned from track or api methods, you don't need to make any changed. `message`, `statusCode`, `response`, and `body` are still accessible as properties on the error.

- (**BREAKING**) `trackAnonymous` now requires an `anonymous_id` and cannot trigger campaigns. If you previously used anonymous events to trigger campaigns, you can still do so [directly through the API](https://customer.io/docs/api/#operation/trackAnonymous). We now refer to anonymous events that trigger campaigns as "invite events".
- (**BREAKING**) `trackAnonymous` now requires an `anonymous_id` and cannot trigger campaigns. If you previously used anonymous events to trigger campaigns, you can still do so [directly through the API](https://customer.io/docs/api/#operation/trackAnonymous). We now refer to anonymous events that trigger campaigns as "invite events".

- (**BREAKING**) Restructure the package to have a single entry point, rather than three. This is more of a standard package structure, and is more future-proof. ([#63](https://github.com/customerio/customerio-node/pull/63))

- Return a readable message when the server returns an array of errors instead of `Unknown error` ([#62](https://github.com/customerio/customerio-node/pull/62))


## [2.1.1]

### Changed

- Fix exported typings for folks using `customerio-node` with Typescript ([#56](https://github.com/customerio/customerio-node/pull/56))

## [2.1.0]

### Changed

- Upgrade `ini` from 1.3.5 to 1.3.8 ([#36](https://github.com/customerio/customerio-node/pull/36))

### Added

- Convert `customerio-node` to Typescript ([#49](https://github.com/customerio/customerio-node/pull/49))

## [2.0.0]

### Changed

- (Breaking) Move triggerBroadcast method from Track to API class ([#46](https://github.com/customerio/customerio-node/pull/46))

## [1.1.0]

### Added

- Support for the EU region

## [1.0.0]

### Added

- Support for the Transactional API

### Removed

- `addToSegment` and `removeFromSegment` methods

### Changed

- IDs in the URLs are now escaped.
- Improved validations for data that's passed in.

## [0.7.0]

### Changed

- Catch scenarios where a response body is unexpectedly `null` ([#25](https://github.com/customerio/customerio-node/pull/25))

### Added

- Allow request defaults to be overridden ([#26](https://github.com/customerio/customerio-node/pull/26))
- New API call for supressing customers ([#27](https://github.com/customerio/customerio-node/pull/27))

## [0.6.0]

### Changed

- Add missing API params to `triggerBroadcast` ([#19](https://github.com/customerio/customerio-node/pull/19))
- Further improve the `triggerBroadcast` API call and catch additional params ([#20](https://github.com/customerio/customerio-node/pull/20))
- Switch from Travis CI to Circle CI ([#21](https://github.com/customerio/customerio-node/pull/21))

## [0.5.0]

### Added

- New API calls for manual segments (`addToSegment`, `removeFromSegment`) ([#16](https://github.com/customerio/customerio-node/pull/16))

## [0.4.0]

### Added

- New API call for adding and removing devices from push notifications ([#14](https://github.com/customerio/customerio-node/pull/14))

## [0.3.0]

### Changed

- Huge thanks to [@jescalan](https://github.com/jescalan) for his work in modernizing the Javascript to es6 along with updating dependencies. ([#13](https://github.com/customerio/customerio-node/pull/13))
- README now has the correct Travis-CI badge
- README has standardized and expanded examples ([#10](https://github.com/customerio/customerio-node/issues/10))
- Fixed link in README to official Customer.io API docs
- Cleaned up .gitignore by removing unnecessary ignore statements

### Added

- This CHANGELOG file along with historical changes to provide better transparancy to changes made to the library
- New API call for API triggered broadcasts
- Added a test for the new call
Expand All @@ -125,14 +150,19 @@ All notable changes to this project will be documented in this file.
- Travis-CI builds now use currently maintained LTS versions of Node.JS (6, 8, 9)

### Removed

- .gitkeep files no longer necessary to preserve directories

## [0.2.0] - 2015-07-22

### Removed

- url.resolve() removed from API calls

## [0.1.0] - 2015-07-22

### Added

- Initial API client library
- Create Identify call
- Create Track call
Expand All @@ -147,8 +177,7 @@ All notable changes to this project will be documented in this file.
- Create DELETE request
- Test suite for middleware


[Unreleased]: https://github.com/customerio/customerio-node/compare/v1.0.0...HEAD
[unreleased]: https://github.com/customerio/customerio-node/compare/v1.0.0...HEAD
[0.3.0]: https://github.com/customerio/customerio-node/compare/d3df250...v0.3.0
[0.2.0]: https://github.com/customerio/customerio-node/compare/54e7c68...d3df250
[0.1.0]: https://github.com/customerio/customerio-node/compare/943668e...54e7c68
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[![ci](https://github.com/customerio/customerio-node/actions/workflows/main.yml/badge.svg)](https://github.com/customerio/customerio-node/actions/workflows/main.yml)

# Customer.io Node

A node client for the Customer.io [REST API](https://customer.io/docs/api/).

## React Native and alternative Node runtimes
Expand Down Expand Up @@ -106,14 +107,16 @@ The first and third parameters represent the identifier for the primary and seco
// cio.mergeCustomers("primaryType", "primaryIdentifier", "secondaryType", "secondaryIdentifier")
// primaryType / secondaryType are one of "id", "email", or "cio_id"
// primaryIdentifier / secondaryIdentifier are the identifier value corresponding to the type.
cio.mergeCustomers(IdentifierType.Id, "[email protected]", IdentifierType.Email, "[email protected]")
cio.mergeCustomers(IdentifierType.Id, "[email protected]", IdentifierType.Email, "[email protected]");
```

#### Options

- **primaryType**: One of the ID types - "id" / "email" / "cio_id" (required)
- **primaryIdentifier**: Primary profile Identifier, String or number (required)
- **secondaryType**: One of the ID types - "id" / "email" / "cio_id" (required)
- **secondaryIdentifier**: Secondary profile Identifier, String or number (required)

---

### cio.track(id, data)
Expand Down
12 changes: 12 additions & 0 deletions check-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { version as hardCodedVersion } from './lib/version';
import packageJson from './package.json';

if (packageJson.version !== hardCodedVersion) {
console.error(`You need to update \`./lib/version.ts\` to match the version in package.json before committing!
package.json version: \x1B[1;32m${packageJson.version}\x1B[m
hard coded version: \x1B[1;31m${hardCodedVersion}\x1B[m`);
process.exit(1);
}

process.exit(0);
6 changes: 3 additions & 3 deletions lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export class APIClient {

triggerBroadcast(id: string | number, data: RequestData, recipients: Recipients) {
let payload = {};
let customRecipientField = (Object.keys(
BROADCASTS_ALLOWED_RECIPIENT_FIELDS,
) as BroadcastsAllowedRecipientFieldsKeys[]).find((field) => recipients[field]);
let customRecipientField = (
Object.keys(BROADCASTS_ALLOWED_RECIPIENT_FIELDS) as BroadcastsAllowedRecipientFieldsKeys[]
).find((field) => recipients[field]);

if (customRecipientField) {
payload = Object.assign({ data }, filterRecipientsDataForField(recipients, customRecipientField));
Expand Down
21 changes: 4 additions & 17 deletions lib/request.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { request } from 'https';
import type { RequestOptions } from 'https';
import { URL } from 'url';
import { resolve } from 'path';
import { CustomerIORequestError, findPackageJson } from './utils';
import { CustomerIORequestError } from './utils';
import { version } from './version';

export type BasicAuth = {
apikey: string;
Expand All @@ -24,10 +24,9 @@ export interface PushRequestData {
device_id?: string;
event?: 'delivered' | 'opened' | 'converted';
timestamp?: number;
};
}

const TIMEOUT = 10_000;
const PACKAGE_JSON = findPackageJson(resolve(__dirname, '..'));

export default class CIORequest {
apikey?: BasicAuth['apikey'];
Expand Down Expand Up @@ -57,23 +56,11 @@ export default class CIORequest {

options(uri: string, method: RequestOptions['method'], data?: RequestData): RequestHandlerOptions {
const body = data ? JSON.stringify(data) : null;
let libraryVersion = 'Unknown';

try {
let json = JSON.parse(PACKAGE_JSON.toString());

libraryVersion = json.version;
} catch {
console.warn(
'WARN: package.json contents could not be read. Activity source data in Customer.io will be incorrect.',
);
}

const headers = {
Authorization: this.auth,
'Content-Type': 'application/json',
'Content-Length': body ? Buffer.byteLength(body, 'utf8') : 0,
'User-Agent': `Customer.io Node Client/${libraryVersion}`,
'User-Agent': `Customer.io Node Client/${version}`,
};

return { method, uri, headers, body };
Expand Down
2 changes: 1 addition & 1 deletion lib/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Region, RegionUS } from './regions';
import { isEmpty } from './utils';
import { IdentifierType } from './types';

type TrackDefaults = RequestOptions & { region: Region; url?: string; apiUrl?: string; };
type TrackDefaults = RequestOptions & { region: Region; url?: string; apiUrl?: string };

class MissingParamError extends Error {
constructor(param: string) {
Expand Down
30 changes: 0 additions & 30 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { IncomingMessage } from 'http';
import { resolve } from 'path';
import fs from 'fs';

export const isEmpty = (value: unknown) => {
return value === null || value === undefined || (typeof value === 'string' && value.trim() === '');
Expand Down Expand Up @@ -37,31 +35,3 @@ ${json.meta.errors.map((error: string) => ` - ${error}`).join('\n')}`;
this.body = body;
}
}

// Node.js v12 doesn't support the `throwIfNoEntry`
// Once we drop v12, let's remove this function and use `throwIfNoEntry`
const checkIfPathExists = (path: string) => {
try {
let stat = fs.statSync(path);

return stat;
} catch {
return undefined;
}
};

export const findPackageJson = (dirName: string): string => {
const path = resolve(dirName, 'package.json');

if (checkIfPathExists(path) == null) {
const parentPath = resolve(dirName, '..');

if (checkIfPathExists(parentPath) != null) {
return findPackageJson(parentPath);
}

return '';
}

return fs.readFileSync(path).toString();
};
1 change: 1 addition & 0 deletions lib/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const version = '3.2.0';
Loading

0 comments on commit 730c64d

Please sign in to comment.