Skip to content

Commit

Permalink
feat: provide auth header
Browse files Browse the repository at this point in the history
  • Loading branch information
ttypic committed Jan 2, 2024
1 parent 877a13f commit cc895ad
Show file tree
Hide file tree
Showing 10 changed files with 1,361 additions and 126 deletions.
2 changes: 2 additions & 0 deletions __mocks__/ably/promises/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class MockRealtime {
};
public auth: {
clientId: string;
requestToken(): void;
};
public connection: {
id?: string;
Expand All @@ -65,6 +66,7 @@ class MockRealtime {
};
this.auth = {
clientId: MOCK_CLIENT_ID,
requestToken: () => {},
};
this.connection = {
id: '1',
Expand Down
1 change: 0 additions & 1 deletion demo/api/conversations/.env.example

This file was deleted.

86 changes: 34 additions & 52 deletions demo/api/conversations/index.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,50 @@
import * as dotenv from 'dotenv';
import * as Ably from 'ably/promises';
import { HandlerEvent } from '@netlify/functions';
import { ulid } from 'ulidx';
import express, { Router } from 'express';
import serverless from 'serverless-http';

dotenv.config();

const messages = [];

export async function handler(event: HandlerEvent) {
if (!process.env.ABLY_API_KEY) {
console.error(`
Missing ABLY_API_KEY environment variable.
If you're running locally, please ensure you have a ./.env file with a value for ABLY_API_KEY=your-key.
If you're running in Netlify, make sure you've configured env variable ABLY_API_KEY.
const api = express();

const router = Router();
router.post('/conversations/:conversationId/messages', (req, res) => {
const conversationId = req.params.conversationId;
const ablyToken = req.headers.authorization.split(' ')[1];

const message = {
id: ulid(),
...JSON.parse(req.body),
client_id: req.headers['ably-clientid'],
conversation_id: conversationId,
reactions: {
counts: {},
latest: [],
mine: [],
},
created_at: Date.now(),
updated_at: null,
deleted_at: null,
};

Please see README.md for more details on configuring your Ably API Key.`);
messages.push(message);

return {
statusCode: 500,
headers: { 'content-type': 'application/json' },
body: JSON.stringify('ABLY_API_KEY is not set'),
};
}
const client = new Ably.Rest(ablyToken);

if (/\/api\/conversations\/v1\/conversations\/(\w+)\/messages/.test(event.path) && event.httpMethod === 'POST') {
const conversationId = /\/api\/conversations\/v1\/conversations\/(\w+)\/messages/.exec(event.path)[1];
const message = {
id: ulid(),
...JSON.parse(event.body),
client_id: event.headers['ably-clientid'],
conversation_id: conversationId,
reactions: {
counts: {},
latest: [],
mine: [],
},
created_at: Date.now(),
updated_at: null,
deleted_at: null,
};
messages.push(message);
client.channels.get(`conversations:${conversationId}`).publish('message.created', message);

const client = new Ably.Rest(process.env.ABLY_API_KEY);
res.json({ id: message.id });

client.channels.get(`conversations:${conversationId}`).publish('message.created', message);
res.status(201).end();
});

return {
statusCode: 201,
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ id: message.id }),
};
}
router.get('/conversations/:conversationId/messages', (req, res) => {
res.json(messages);
});

const getMessagesRegEx = /\/api\/conversations\/v1\/conversations\/(\w+)\/messages/;
if (getMessagesRegEx.test(event.path) && event.httpMethod === 'GET') {
return {
statusCode: 200,
headers: { 'content-type': 'application/json' },
body: JSON.stringify(messages),
};
}
api.use('/api/conversations/v1', router);

return {
statusCode: 404,
body: 'Not Found',
};
}
export const handler = serverless(api);
Loading

0 comments on commit cc895ad

Please sign in to comment.