@push-rpc/next
Advanced tools
Comparing version 2.0.2 to 2.0.3
export declare class HttpClient { | ||
private url; | ||
private clientId; | ||
constructor(url: string, clientId: string); | ||
private getHeaders; | ||
constructor(url: string, clientId: string, getHeaders: () => Promise<Record<string, string>>); | ||
call(itemName: string, params: unknown[], callTimeout: number): Promise<unknown>; | ||
@@ -6,0 +7,0 @@ subscribe(itemName: string, params: unknown[], callTimeout: number): Promise<unknown>; |
@@ -7,14 +7,15 @@ "use strict"; | ||
class HttpClient { | ||
constructor(url, clientId) { | ||
constructor(url, clientId, getHeaders) { | ||
this.url = url; | ||
this.clientId = clientId; | ||
this.getHeaders = getHeaders; | ||
} | ||
call(itemName, params, callTimeout) { | ||
return this.httpRequest("POST", itemName, params, callTimeout); | ||
async call(itemName, params, callTimeout) { | ||
return this.httpRequest("POST", itemName, params, callTimeout, await this.getHeaders()); | ||
} | ||
async subscribe(itemName, params, callTimeout) { | ||
return this.httpRequest("PUT", itemName, params, callTimeout); | ||
return this.httpRequest("PUT", itemName, params, callTimeout, await this.getHeaders()); | ||
} | ||
async unsubscribe(itemName, params, callTimeout) { | ||
await this.httpRequest("PATCH", itemName, params, callTimeout); | ||
await this.httpRequest("PATCH", itemName, params, callTimeout, await this.getHeaders()); | ||
} | ||
@@ -24,3 +25,3 @@ getItemUrl(itemName) { | ||
} | ||
async httpRequest(method, itemName, params, callTimeout) { | ||
async httpRequest(method, itemName, params, callTimeout, headers) { | ||
try { | ||
@@ -32,2 +33,3 @@ const response = await fetch(this.getItemUrl(itemName), { | ||
[rpc_js_1.CLIENT_ID_HEADER]: this.clientId, | ||
...headers, | ||
}, | ||
@@ -34,0 +36,0 @@ body: (0, json_js_1.safeStringify)(params), |
@@ -18,2 +18,5 @@ import { RpcContext, Services } from "../rpc.js"; | ||
connectOnCreate: boolean; | ||
onConnected: () => void; | ||
onDisconnected: () => void; | ||
getHeaders: () => Promise<Record<string, string>>; | ||
}; | ||
@@ -20,0 +23,0 @@ export declare function consumeServices<S extends Services<S>>(url: string, overrideOptions?: Partial<ConsumeServicesOptions>): Promise<{ |
@@ -31,3 +31,6 @@ "use strict"; | ||
connectOnCreate: false, | ||
onConnected: () => { }, | ||
onDisconnected: () => { }, | ||
getHeaders: async () => ({}), | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -47,3 +47,3 @@ "use strict"; | ||
}; | ||
this.httpClient = new HttpClient_js_1.HttpClient(url, this.clientId); | ||
this.httpClient = new HttpClient_js_1.HttpClient(url, this.clientId, options.getHeaders); | ||
this.remoteSubscriptions = new RemoteSubscriptions_js_1.RemoteSubscriptions(); | ||
@@ -57,3 +57,8 @@ this.connection = new WebSocketConnection_js_1.WebSocketConnection(url, this.clientId, { | ||
this.remoteSubscriptions.consume(itemName, parameters, data); | ||
}, this.resubscribe); | ||
}, () => { | ||
this.resubscribe(); | ||
options.onConnected(); | ||
}, () => { | ||
options.onDisconnected(); | ||
}); | ||
} | ||
@@ -60,0 +65,0 @@ isConnected() { |
@@ -7,2 +7,3 @@ export declare class WebSocketConnection { | ||
private readonly onConnected; | ||
private readonly onDisconnected; | ||
constructor(url: string, clientId: string, options: { | ||
@@ -13,3 +14,3 @@ subscriptions: boolean; | ||
pingInterval: number | null; | ||
}, consume: (itemName: string, parameters: unknown[], data: unknown) => void, onConnected: () => void); | ||
}, consume: (itemName: string, parameters: unknown[], data: unknown) => void, onConnected: () => void, onDisconnected: () => void); | ||
close(): Promise<void>; | ||
@@ -16,0 +17,0 @@ /** |
@@ -8,3 +8,3 @@ "use strict"; | ||
class WebSocketConnection { | ||
constructor(url, clientId, options, consume, onConnected) { | ||
constructor(url, clientId, options, consume, onConnected, onDisconnected) { | ||
this.url = url; | ||
@@ -15,2 +15,3 @@ this.clientId = clientId; | ||
this.onConnected = onConnected; | ||
this.onDisconnected = onDisconnected; | ||
this.socket = null; | ||
@@ -107,2 +108,3 @@ this.disconnectedMark = true; | ||
onDisconnected(); | ||
this.onDisconnected(); | ||
} | ||
@@ -109,0 +111,0 @@ if (this.pingTimeout) { |
@@ -6,3 +6,3 @@ "use strict"; | ||
const logger_js_1 = require("../logger.js"); | ||
const server_1 = require("../utils/server"); | ||
const server_js_1 = require("../utils/server.js"); | ||
async function serveHttpRequest(req, res, path, hooks, createConnectionContext) { | ||
@@ -76,3 +76,3 @@ // if port is in options - response 404 on other URLs | ||
function readBody(req) { | ||
const decompressed = (0, server_1.decompressRequest)(req); | ||
const decompressed = (0, server_js_1.decompressRequest)(req); | ||
return new Promise((resolve, reject) => { | ||
@@ -79,0 +79,0 @@ let body = ""; |
{ | ||
"name": "@push-rpc/next", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
@@ -7,15 +7,16 @@ import {CLIENT_ID_HEADER, RpcErrors} from "../rpc.js" | ||
private url: string, | ||
private clientId: string | ||
private clientId: string, | ||
private getHeaders: () => Promise<Record<string, string>> | ||
) {} | ||
call(itemName: string, params: unknown[], callTimeout: number): Promise<unknown> { | ||
return this.httpRequest("POST", itemName, params, callTimeout) | ||
async call(itemName: string, params: unknown[], callTimeout: number): Promise<unknown> { | ||
return this.httpRequest("POST", itemName, params, callTimeout, await this.getHeaders()) | ||
} | ||
async subscribe(itemName: string, params: unknown[], callTimeout: number) { | ||
return this.httpRequest("PUT", itemName, params, callTimeout) | ||
return this.httpRequest("PUT", itemName, params, callTimeout, await this.getHeaders()) | ||
} | ||
async unsubscribe(itemName: string, params: unknown[], callTimeout: number) { | ||
await this.httpRequest("PATCH", itemName, params, callTimeout) | ||
await this.httpRequest("PATCH", itemName, params, callTimeout, await this.getHeaders()) | ||
} | ||
@@ -31,3 +32,4 @@ | ||
params: unknown[], | ||
callTimeout: number | ||
callTimeout: number, | ||
headers: Record<string, string> | ||
): Promise<unknown> { | ||
@@ -40,2 +42,3 @@ try { | ||
[CLIENT_ID_HEADER]: this.clientId, | ||
...headers, | ||
}, | ||
@@ -42,0 +45,0 @@ body: safeStringify(params), |
@@ -23,2 +23,5 @@ import {RpcContext, Services} from "../rpc.js" | ||
connectOnCreate: boolean | ||
onConnected: () => void | ||
onDisconnected: () => void | ||
getHeaders: () => Promise<Record<string, string>> | ||
} | ||
@@ -62,2 +65,5 @@ | ||
connectOnCreate: false, | ||
onConnected: () => {}, | ||
onDisconnected: () => {}, | ||
getHeaders: async () => ({}), | ||
} |
@@ -15,3 +15,3 @@ import {CallOptions, InvocationType, RpcContext, Services} from "../rpc.js" | ||
) { | ||
this.httpClient = new HttpClient(url, this.clientId) | ||
this.httpClient = new HttpClient(url, this.clientId, options.getHeaders) | ||
this.remoteSubscriptions = new RemoteSubscriptions() | ||
@@ -31,3 +31,9 @@ | ||
}, | ||
this.resubscribe | ||
() => { | ||
this.resubscribe() | ||
options.onConnected() | ||
}, | ||
() => { | ||
options.onDisconnected() | ||
} | ||
) | ||
@@ -34,0 +40,0 @@ } |
@@ -16,3 +16,4 @@ import {log} from "../logger.js" | ||
private readonly consume: (itemName: string, parameters: unknown[], data: unknown) => void, | ||
private readonly onConnected: () => void | ||
private readonly onConnected: () => void, | ||
private readonly onDisconnected: () => void | ||
) { | ||
@@ -133,2 +134,3 @@ this.url = url.replace(/^https(.*)/, "wss$1").replace(/^http(.*)/, "ws$1") | ||
onDisconnected() | ||
this.onDisconnected() | ||
} | ||
@@ -135,0 +137,0 @@ |
@@ -6,3 +6,3 @@ import * as http from "http" | ||
import {log} from "../logger.js" | ||
import {decompressRequest} from "../utils/server" | ||
import {decompressRequest} from "../utils/server.js" | ||
@@ -9,0 +9,0 @@ export async function serveHttpRequest( |
@@ -107,2 +107,30 @@ import {assert} from "chai" | ||
}) | ||
it("calls listeners", async () => { | ||
const services = await startTestServer({ | ||
item: async () => "1", | ||
}) | ||
let connected = false | ||
let disconnected = false | ||
const remote = await createTestClient<typeof services>({ | ||
onConnected: () => { | ||
connected = true | ||
}, | ||
onDisconnected: () => { | ||
disconnected = true | ||
}, | ||
}) | ||
await remote.item.subscribe(() => {}) | ||
assert.equal(connected, true) | ||
await testClient!.close() | ||
await adelay(10) | ||
assert.equal(disconnected, true) | ||
}) | ||
}) |
import {assert} from "chai" | ||
import {createTestClient, startTestServer, TEST_PORT} from "./testUtils.js" | ||
import {RpcErrors} from "../src/index.js" | ||
import {RpcConnectionContext, RpcErrors} from "../src/index.js" | ||
import {IncomingMessage} from "http" | ||
@@ -207,2 +208,36 @@ describe("Misc", () => { | ||
}) | ||
it("pass headers to server", async () => { | ||
let userName: string | undefined | ||
const services = await startTestServer( | ||
{ | ||
async hello1(ctx?) { | ||
userName = ctx.userName | ||
}, | ||
}, | ||
{ | ||
async createConnectionContext( | ||
req: IncomingMessage | ||
): Promise<RpcConnectionContext & {userName: string}> { | ||
return { | ||
clientId: "test", | ||
userName: req.headers["x-user-name"] as string, | ||
} | ||
}, | ||
} | ||
) | ||
const remote = await createTestClient<typeof services>({ | ||
async getHeaders() { | ||
return { | ||
["X-User-Name"]: "testUser", | ||
} | ||
}, | ||
}) | ||
await remote.hello1() | ||
assert.equal(userName, "testUser") | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
196231
4373