Skip to content

Commit fcc5618

Browse files
authored
Merge pull request #4 from workos/feature/sup-596-typescript-update-directory-sync-app-to
Feature/sup 596 typescript update directory sync app to es6 syntax
2 parents 87dcc91 + 069b526 commit fcc5618

File tree

7 files changed

+122
-131
lines changed

7 files changed

+122
-131
lines changed

typescript-directory-sync-example/README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# TypeScript Example App with Directory Sync powered by WorkOS
22

3-
An example application demonstrating to use TypeScript with the [WorkOS Node SDK](https://github.com/workos/workos-node) to power Directory Sync.
3+
An example application demonstrating to use TypeScript with the [WorkOS Node SDK](https://github.com/workos/workos-node) to power Directory Sync.
44

55
## Prerequisites
66

@@ -9,39 +9,41 @@ Node.js version 10+
99
## Node Project Setup
1010

1111
1. Clone the main repo and install dependencies for the app you'd like to use:
12+
1213
```bash
1314
# HTTPS
1415
git clone https://github.com/workos/typescript-example-applications.git
1516
```
17+
1618
or
1719

1820
```bash
1921
# SSH
2022
git clone git@github.com:workos/typescript-example-applications.git
2123
```
2224

23-
2. Navigate to Directory Sync app within the cloned repo.
24-
```bash
25-
$ cd typescript-example-applications/typescript-directory-sync-example
26-
```
25+
2. Navigate to Directory Sync app within the cloned repo.
2726

28-
3. Install the dependencies.
2927
```bash
30-
$ npm install
28+
$ cd typescript-example-applications/typescript-directory-sync-example
3129
```
3230

31+
3. Install the dependencies.
32+
```bash
33+
$ npm install
34+
```
3335

3436
## Configure your environment
3537

3638
1. Grab your [API Key](https://dashboard.workos.com/api-keys).
3739
2. Get your [Client ID](https://dashboard.workos.com/sso/configuration).
3840
3. Create a `.env` file at the root of the project and populate with the
39-
following environment variables (using values found above):
41+
following environment variables (using values found above):
4042

4143
```typescript
42-
WORKOS_API_KEY=your_api_key_here
43-
WORKOS_CLIENT_ID=your_project_id_here
44-
PORT=3000
44+
WORKOS_API_KEY = your_api_key_here
45+
WORKOS_CLIENT_ID = your_project_id_here
46+
PORT = 8000
4547
```
4648

4749
## Run the server
@@ -50,13 +52,12 @@ PORT=3000
5052
npm run dev
5153
```
5254

53-
Head to `http://localhost:3000/` to navigate your directories!
55+
Head to `http://localhost:8000/` to navigate your directories!
5456

5557
## Testing Webhooks
5658

5759
### 1. Click on the "Test Webhooks" button to navigate to the webhooks view.
5860

59-
6061
### 2. Start an `ngrok` session
6162

6263
[Ngrok](https://ngrok.com/) is a simple application that allows you to map a local endpoint to a public endpoint.
@@ -83,7 +84,6 @@ Then populate the following environment variable in your `.env` file at the root
8384
WORKOS_WEBHOOK_SECRET=your_webhook_secret
8485
```
8586

86-
8787
## Need help?
8888

8989
If you get stuck and aren't able to resolve the issue by reading our [WorkOS Directory Sync documentation](https://workos.com/docs/directory-sync/guide), API reference, or tutorials, you can reach out to us at support@workos.com and we'll lend a hand.

typescript-directory-sync-example/dist/index.js

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1313
};
1414
Object.defineProperty(exports, "__esModule", { value: true });
1515
const express_1 = __importDefault(require("express"));
16-
const dotenv_1 = __importDefault(require("dotenv"));
17-
const path = require("path");
18-
const WorkOS = require('@workos-inc/node').default;
19-
var cookieParser = require('cookie-parser');
20-
var logger = require('morgan');
16+
require("dotenv/config");
17+
const node_1 = require("@workos-inc/node");
18+
const socket_io_1 = require("socket.io");
19+
const morgan_1 = __importDefault(require("morgan"));
2120
process.on('unhandledRejection', (reason, p) => { throw reason; });
22-
dotenv_1.default.config();
2321
const app = (0, express_1.default)();
24-
const port = process.env.PORT;
25-
const workos = new WorkOS(process.env.WORKOS_API_KEY);
22+
const port = process.env.PORT || '8000';
23+
const workos = new node_1.WorkOS(process.env.WORKOS_API_KEY);
24+
app.use('/public', express_1.default.static('public'));
25+
app.use(express_1.default.urlencoded({ extended: true }));
26+
app.set('view engine', 'ejs');
27+
app.use(express_1.default.json());
28+
app.use((0, morgan_1.default)('dev'));
2629
const server = app.listen(port, () => {
2730
console.log(`⚡️[server]: Server is running at https://localhost:${port}`);
2831
});
29-
const io = require('socket.io')(server);
30-
// app.engine('html', require('ejs').renderFile);
31-
app.set('views', './views');
32-
app.set('view engine', 'ejs');
33-
app.use(express_1.default.static(path.join(__dirname, "public")));
34-
app.use(logger('dev'));
35-
app.use(express_1.default.json());
36-
app.use(express_1.default.urlencoded({ extended: false }));
37-
app.use(cookieParser());
38-
app.use(express_1.default.static(path.join(__dirname)));
39-
app.use('/public', express_1.default.static('public'));
32+
const io = new socket_io_1.Server(server);
4033
io.on('connection', (socket) => {
4134
console.log('connected');
4235
socket.on('disconnect', () => {
@@ -63,12 +56,12 @@ app.get('/directory/:id', (req, res) => __awaiter(void 0, void 0, void 0, functi
6356
app.post('/webhooks', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
6457
const webhook = workos.webhooks.constructEvent({
6558
payload: req.body,
66-
sigHeader: req.headers['workos-signature'],
67-
secret: process.env.WORKOS_WEBHOOK_SECRET,
59+
sigHeader: typeof req.headers['workos-signature'] === 'string' ? req.headers['workos-signature'] : "",
60+
secret: process.env.WORKOS_WEBHOOK_SECRET !== undefined ? process.env.WORKOS_WEBHOOK_SECRET : "",
6861
tolerance: 90000,
6962
});
7063
io.emit('webhook event', { webhook });
71-
return 200;
64+
res.sendStatus(200);
7265
}));
7366
app.get('/webhooks', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
7467
res.render('webhooks.ejs', {

typescript-directory-sync-example/index.ts

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
1-
import express, { Express, Request, Response } from 'express';
2-
import dotenv from 'dotenv';
3-
import { Socket } from 'socket.io';
4-
import { Directory, Group, User } from '@workos-inc/node';
5-
const path = require("path");
6-
const WorkOS = require('@workos-inc/node').default;
7-
var cookieParser = require('cookie-parser');
8-
var logger = require('morgan');
1+
import express, { Express } from 'express'
2+
import 'dotenv/config'
3+
import { WorkOS, Directory, Group, User } from '@workos-inc/node'
4+
import { Server, Socket } from 'socket.io'
5+
import morgan from 'morgan'
96
process.on('unhandledRejection', (reason, p) => { throw reason });
10-
dotenv.config();
117

128
const app: Express = express();
13-
const port = process.env.PORT;
14-
const workos = new WorkOS(process.env.WORKOS_API_KEY);
9+
const port = process.env.PORT || '8000'
10+
const workos = new WorkOS(process.env.WORKOS_API_KEY)
11+
12+
app.use('/public', express.static('public'))
13+
app.use(express.urlencoded({ extended: true }))
14+
app.set('view engine', 'ejs');
15+
app.use(express.json())
16+
app.use(morgan('dev'))
1517

1618
const server = app.listen(port, () => {
1719
console.log(`⚡️[server]: Server is running at https://localhost:${port}`);
1820
});
1921

20-
const io = require('socket.io')(server);
21-
22-
23-
// app.engine('html', require('ejs').renderFile);
24-
app.set('views', './views');
25-
app.set('view engine', 'ejs');
26-
app.use(express.static(path.join(__dirname, "public")));
27-
28-
app.use(logger('dev'));
29-
app.use(express.json());
30-
app.use(express.urlencoded({ extended: false }));
31-
app.use(cookieParser());
32-
app.use(express.static(path.join(__dirname)));
33-
app.use('/public', express.static('public'));
22+
const io = new Server(server)
3423

3524
io.on('connection', (socket: Socket) => {
3625
console.log('connected');
@@ -48,7 +37,6 @@ app.get("/", async (req, res) => {
4837
});
4938
});
5039

51-
5240
app.get('/directory/:id', async (req, res) => {
5341
const directories = await workos.directorySync.listDirectories();
5442
const directory = await directories.data.filter((directory: Directory) => {
@@ -63,13 +51,13 @@ app.get('/directory/:id', async (req, res) => {
6351
app.post('/webhooks', async (req, res) => {
6452
const webhook = workos.webhooks.constructEvent({
6553
payload: req.body,
66-
sigHeader: req.headers['workos-signature'],
67-
secret: process.env.WORKOS_WEBHOOK_SECRET,
54+
sigHeader: typeof req.headers['workos-signature'] === 'string' ? req.headers['workos-signature'] : "",
55+
secret: process.env.WORKOS_WEBHOOK_SECRET !== undefined ? process.env.WORKOS_WEBHOOK_SECRET : "",
6856
tolerance: 90000,
6957
})
7058
io.emit('webhook event', { webhook })
7159

72-
return 200;
60+
res.sendStatus(200)
7361
})
7462

7563
app.get('/webhooks', async (req, res) => {

0 commit comments

Comments
 (0)