Skip to content

Commit

Permalink
feat: include SDK version in the auth call and improve config (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavkam committed Jan 24, 2024
1 parent 250e6fb commit db076b3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@bucketco/tracking-sdk",
"version": "2.1.9",
"version": "2.1.10",
"license": "MIT",
"private": false,
"repository": {
Expand Down
6 changes: 6 additions & 0 deletions src/config.ts
@@ -1,2 +1,8 @@
import { version } from "../package.json";

export const TRACKING_HOST = "https://tracking.bucket.co";
export const SSE_REALTIME_HOST = "https://livemessaging.bucket.co";

export const SDK_VERSION_HEADER_NAME = "Bucket-Sdk-Version";

export const SDK_VERSION = version;
12 changes: 8 additions & 4 deletions src/main.ts
@@ -1,10 +1,13 @@
import fetch from "cross-fetch";
import { isForNode } from "is-bundling-for-browser-or-node";

import { version } from "../package.json";

import type { FeedbackPosition, FeedbackTranslations } from "./feedback/types";
import { SSE_REALTIME_HOST, TRACKING_HOST } from "./config";
import {
SDK_VERSION,
SDK_VERSION_HEADER_NAME,
SSE_REALTIME_HOST,
TRACKING_HOST,
} from "./config";
import { createDefaultFeedbackPromptHandler } from "./default-feedback-prompt-handler";
import * as feedbackLib from "./feedback";
import { getAuthToken } from "./prompt-storage";
Expand Down Expand Up @@ -34,7 +37,7 @@ async function request(url: string, body: any) {
method: "post",
headers: {
"Content-Type": "application/json",
"Bucket-Sdk-Version": version,
[SDK_VERSION_HEADER_NAME]: SDK_VERSION,
},
body: JSON.stringify(body),
});
Expand Down Expand Up @@ -576,6 +579,7 @@ export default function main() {
// lifecycle
init,
reset,
version: SDK_VERSION,
// requests
user,
company,
Expand Down
7 changes: 6 additions & 1 deletion src/sse.ts
@@ -1,6 +1,10 @@
import fetch from "cross-fetch";

import { SSE_REALTIME_HOST } from "./config";
import {
SDK_VERSION,
SDK_VERSION_HEADER_NAME,
SSE_REALTIME_HOST,
} from "./config";
import {
forgetAuthToken,
getAuthToken,
Expand Down Expand Up @@ -62,6 +66,7 @@ export class AblySSEChannel {
method: "get",
headers: {
"Content-Type": "application/json",
[SDK_VERSION_HEADER_NAME]: SDK_VERSION,
},
},
);
Expand Down
14 changes: 12 additions & 2 deletions test/usage.test.ts
Expand Up @@ -12,7 +12,11 @@ import {
} from "vitest";

import { version } from "../package.json";
import { TRACKING_HOST } from "../src/config";
import {
SDK_VERSION,
SDK_VERSION_HEADER_NAME,
TRACKING_HOST,
} from "../src/config";
import bucket from "../src/main";
import {
checkPromptMessageCompleted,
Expand Down Expand Up @@ -208,7 +212,7 @@ describe("usage", () => {
test("will send sdk version as header", async () => {
nock(`${TRACKING_HOST}/${KEY}`, {
reqheaders: {
"Bucket-Sdk-Version": version,
[SDK_VERSION_HEADER_NAME]: version,
},
})
.post(/.*\/user/, {
Expand All @@ -221,6 +225,12 @@ describe("usage", () => {
await bucketInstance.user("foo");
});

test("will send sdk version as header", async () => {
const bucketInstance = bucket();

expect(bucketInstance.version).toBe(SDK_VERSION);
});

test("can reset user", async () => {
nock(`${TRACKING_HOST}/${KEY}`)
.post(/.*\/user/, {
Expand Down

0 comments on commit db076b3

Please sign in to comment.