Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions packages/core/realtime-js/src/RealtimeChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,21 +554,22 @@ export default class RealtimeChannel {
payload: any,
opts: { timeout?: number } = {}
): Promise<{ success: true } | { success: false; status: number; error: string }> {
const authorization = this.socket.accessTokenValue
? `Bearer ${this.socket.accessTokenValue}`
: ''

if (payload === undefined || payload === null) {
return Promise.reject('Payload is required for httpSend()')
}

const headers: Record<string, string> = {
apikey: this.socket.apiKey ? this.socket.apiKey : '',
'Content-Type': 'application/json',
}

if (this.socket.accessTokenValue) {
headers['Authorization'] = `Bearer ${this.socket.accessTokenValue}`
}

const options = {
method: 'POST',
headers: {
Authorization: authorization,
apikey: this.socket.apiKey ? this.socket.apiKey : '',
'Content-Type': 'application/json',
},
headers,
body: JSON.stringify({
messages: [
{
Expand Down Expand Up @@ -626,16 +627,18 @@ export default class RealtimeChannel {
)

const { event, payload: endpoint_payload } = args
const authorization = this.socket.accessTokenValue
? `Bearer ${this.socket.accessTokenValue}`
: ''
const headers: Record<string, string> = {
apikey: this.socket.apiKey ? this.socket.apiKey : '',
'Content-Type': 'application/json',
}

if (this.socket.accessTokenValue) {
headers['Authorization'] = `Bearer ${this.socket.accessTokenValue}`
}

const options = {
method: 'POST',
headers: {
Authorization: authorization,
apikey: this.socket.apiKey ? this.socket.apiKey : '',
'Content-Type': 'application/json',
},
headers,
body: JSON.stringify({
messages: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ describe('send', () => {
{
description: 'without access token',
accessToken: undefined,
expectedAuth: '',
expectedAuth: undefined,
},
{
description: 'with access token',
Expand Down Expand Up @@ -363,13 +363,17 @@ describe('send', () => {
config: { private: true },
})

const expectedHeaders: Record<string, string> = {
apikey: 'abc123',
'Content-Type': 'application/json',
}
if (expectedAuth) {
expectedHeaders['Authorization'] = expectedAuth
}

const expectedBody = {
method: 'POST',
headers: {
Authorization: expectedAuth,
apikey: 'abc123',
'Content-Type': 'application/json',
},
headers: expectedHeaders,
body: '{"messages":[{"topic":"topic","event":"test","private":true}]}',
signal: new AbortController().signal,
}
Expand Down Expand Up @@ -498,12 +502,12 @@ describe('httpSend', () => {
{
name: 'without access token',
hasToken: false,
expectedAuth: '',
expectedAuth: undefined as string | undefined,
},
{
name: 'with access token',
hasToken: true,
expectedAuth: 'Bearer token123',
expectedAuth: 'Bearer token123' as string | undefined,
},
]

Expand Down
Loading