Skip to content

Commit 70f5a34

Browse files
committed
Initial commit - linting in progress!
0 parents  commit 70f5a34

File tree

10 files changed

+1747
-0
lines changed

10 files changed

+1747
-0
lines changed

.env

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export SF_URL=
2+
export SF_USERNAME=
3+
export SF_PASS=
4+
export SF_TOKEN=
5+
export SF_CLIENT_SECRET=
6+
export SF_CLIENT_ID=
7+
export SF_EVENT=

.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: ['airbnb-typescript'],
3+
parserOptions: {
4+
project: './tsconfig.json',
5+
},
6+
};

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "platform-event-consumer",
3+
"version": "1.0.0",
4+
"description": "A package to consume Platform Events from Salesforce",
5+
"main": "index.ts",
6+
"scripts": {
7+
"build": "rm -rf ./dist && tsc",
8+
"test:lint": "eslint --ext .js,.ts .",
9+
"start": "node dist/index.js"
10+
},
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"@salesforce/ts-types": "^1.2.2",
15+
"@types/node": "^14.0.5",
16+
"cometd": "^5.0.0",
17+
"cometd-nodejs-client": "^1.1.0",
18+
"ts-force": "^3.0.0-rc.3"
19+
},
20+
"devDependencies": {
21+
"@types/cometd": "^4.0.7",
22+
"@typescript-eslint/eslint-plugin": "^2.24.0",
23+
"eslint": "^7.0.0",
24+
"eslint-config-airbnb-typescript": "^7.2.1",
25+
"eslint-plugin-import": "^2.20.1",
26+
"eslint-plugin-jsx-a11y": "^6.2.3",
27+
"eslint-plugin-react": "^7.19.0",
28+
"json-schema": "^0.2.5",
29+
"typescript": "^3.8.3"
30+
}
31+
}

src/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
declare module 'cometd-nodejs-client' {
2+
export interface CometDClientOptions {
3+
logLevel?: string,
4+
httpProxy?: HTTPProxyOptions
5+
}
6+
7+
export interface HTTPProxyOptions {
8+
uri?: string,
9+
includes?: boolean,
10+
excludes?: boolean
11+
}
12+
13+
export function adapt(options?: CometDClientOptions): void;
14+
}

src/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { authenticate, subscribe } from './sf';
2+
3+
authenticate(
4+
process.env.SF_USERNAME!,
5+
process.env.SF_PASS!,
6+
process.env.SF_TOKEN!,
7+
process.env.SF_URL!,
8+
process.env.SF_CLIENT_SECRET!,
9+
process.env.SF_CLIENT_ID!,
10+
)
11+
.then((authInfo) => {
12+
subscribe(authInfo.accessToken, authInfo.instanceUrl, process.env.SF_EVENT!);
13+
});
14+
15+
process.stdin.resume();

src/sf/index.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import cometD from 'cometd';
2+
import adapter from 'cometd-nodejs-client';
3+
import { OAuth, UsernamePasswordConfig } from 'ts-force';
4+
5+
const authenticate = async (
6+
username: string,
7+
password: string,
8+
accessToken: string,
9+
loginURL: string,
10+
clientSecret: string,
11+
clientId: string) => {
12+
const config = new UsernamePasswordConfig(
13+
clientId,
14+
clientSecret,
15+
loginURL,
16+
username,
17+
`${password}${accessToken}`,
18+
);
19+
20+
const oAuth = new OAuth(config);
21+
return oAuth.initialize();
22+
};
23+
24+
const subscribe = (
25+
accessToken: string,
26+
sfURL: string,
27+
event: string,
28+
) => {
29+
adapter.adapt();
30+
// Create the CometD object.
31+
const cometd = new cometD.CometD();
32+
33+
// Configure the CometD object.
34+
cometd.configure({
35+
url: `${sfURL}/cometd/48.0/`,
36+
appendMessageTypeToURL: false,
37+
requestHeaders: { Authorization: `OAuth ${accessToken}` },
38+
});
39+
40+
// Handshake with the server.
41+
cometd.handshake((h) => {
42+
if (!h.successful) {
43+
console.error('Unsuccessful handshake');
44+
}
45+
46+
console.log('Handshake succesful');
47+
// Subscribe to receive messages from the server.
48+
cometd.subscribe(`/event/${event}`, (m) => {
49+
const dataFromServer = m.data;
50+
console.log(dataFromServer);
51+
// Use dataFromServer.
52+
});
53+
});
54+
};
55+
56+
export {
57+
authenticate,
58+
subscribe,
59+
};

tsconfig.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"typeRoots": [
4+
"node_modules/@types",
5+
"src/@types",
6+
],
7+
"target": "es6",
8+
"module": "commonjs",
9+
"outDir": "dist",
10+
"noImplicitAny": true,
11+
"strict": true,
12+
"esModuleInterop": true,
13+
"experimentalDecorators": true,
14+
"emitDecoratorMetadata": true
15+
},
16+
"include": [
17+
"src/**/*"
18+
],
19+
"exclude": [
20+
".eslintrc.js",
21+
"node_modules",
22+
"**/__tests__/*"
23+
]
24+
}

0 commit comments

Comments
 (0)