Skip to content

Commit

Permalink
URL parsing fixes (#938)
Browse files Browse the repository at this point in the history
* url parsing fixes

* add test

* add changeset

* update default multipass

* add more tests

* remove console log

* refactor endpoint stubs

* self review fixes

* update OSW

* fix lock file and add changeset

* fix test url

* add test
  • Loading branch information
ssanjay1 authored Nov 21, 2024
1 parent 902e40d commit 91cfa3a
Show file tree
Hide file tree
Showing 25 changed files with 430 additions and 151 deletions.
12 changes: 12 additions & 0 deletions .changeset/great-shoes-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@osdk/foundry-sdk-generator": patch
"@osdk/generator-converters": patch
"@osdk/cli.cmd.typescript": patch
"@osdk/shared.test": patch
"@osdk/generator": patch
"@osdk/client": patch
"@osdk/maker": patch
"@osdk/oauth": patch
---

Fixing url parsing for client.
8 changes: 8 additions & 0 deletions .changeset/popular-forks-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@osdk/shared.net.platformapi": patch
"@osdk/shared.test": patch
"@osdk/client": patch
"@osdk/oauth": patch
---

Fixing URL parsing for custom entry points.
4 changes: 2 additions & 2 deletions packages/cli.cmd.typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"@arethetypeswrong/cli": "^0.15.2",
"@osdk/cli.common": "workspace:~",
"@osdk/generator": "workspace:~",
"@osdk/internal.foundry.core": "2.5.0",
"@osdk/internal.foundry.ontologiesv2": "2.5.0",
"@osdk/internal.foundry.core": "2.6.0-beta.0",
"@osdk/internal.foundry.ontologiesv2": "2.6.0-beta.0",
"@osdk/shared.net": "workspace:~",
"consola": "^3.2.3",
"fast-deep-equal": "^3.1.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"@osdk/api": "workspace:~",
"@osdk/client.unstable": "workspace:*",
"@osdk/generator-converters": "workspace:*",
"@osdk/internal.foundry.core": "2.5.0",
"@osdk/internal.foundry.ontologiesv2": "2.5.0",
"@osdk/internal.foundry.core": "2.6.0-beta.0",
"@osdk/internal.foundry.ontologiesv2": "2.6.0-beta.0",
"@osdk/shared.client": "^1.0.1",
"@osdk/shared.client.impl": "workspace:~",
"@osdk/shared.client2": "^1.0.0",
Expand Down
25 changes: 25 additions & 0 deletions packages/client/src/actions/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { remapActionResponse } from "./applyAction.js";

describe("actions", () => {
let client: Client;
let customEntryPointClient: Client;

beforeAll(async () => {
apiServer.listen();
Expand All @@ -55,6 +56,11 @@ describe("actions", () => {
$ontologyRid,
async () => "myAccessToken",
);
customEntryPointClient = createClient(
"https://stack.palantirCustom.com/foo/first/someStuff",
$ontologyRid,
async () => "myAccessToken",
);
});

afterAll(() => {
Expand Down Expand Up @@ -124,6 +130,25 @@ describe("actions", () => {
`);
});

it("returns validation directly on validateOnly mode, with custom entry point in URL", async () => {
const result = await customEntryPointClient(moveOffice).applyAction({
officeId: "SEA",
newAddress: "456 Pike Place",
newCapacity: 40,
}, {
$validateOnly: true,
});
expectTypeOf<typeof result>().toEqualTypeOf<ActionValidationResponse>();

expect(result).toMatchInlineSnapshot(`
{
"parameters": {},
"result": "INVALID",
"submissionCriteria": [],
}
`);
});

it("throws on validation errors", async () => {
try {
const result = await client(moveOffice).applyAction({
Expand Down
42 changes: 42 additions & 0 deletions packages/client/src/createClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { Task } from "@osdk/client.test.ontology";
import * as SharedClientContext from "@osdk/shared.client.impl";
import { mockFetchResponse, MockOntology } from "@osdk/shared.test";
import type { MockedFunction } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
Expand Down Expand Up @@ -67,4 +68,45 @@ describe(createClient, () => {
]);
});
});

describe("check url formatting", () => {
it("urls are correctly formatted", async () => {
const spy = vi.spyOn(SharedClientContext, "createSharedClientContext");
createClient(
"https://mock.com",
MockOntology.metadata.ontologyRid,
async () => "Token",
undefined,
fetchFunction,
);
expect(spy.mock.calls[0][0]).toBe("https://mock.com/");

createClient(
"https://mock1.com/",
MockOntology.metadata.ontologyRid,
async () => "Token",
undefined,
fetchFunction,
);
expect(spy.mock.calls[1][0]).toBe("https://mock1.com/");

createClient(
"https://mock2.com/stuff/first/foo",
MockOntology.metadata.ontologyRid,
async () => "Token",
undefined,
fetchFunction,
);
expect(spy.mock.calls[2][0]).toBe("https://mock2.com/stuff/first/foo/");

createClient(
"https://mock3.com/stuff/first/foo/",
MockOntology.metadata.ontologyRid,
async () => "Token",
undefined,
fetchFunction,
);
expect(spy.mock.calls[3][0]).toBe("https://mock3.com/stuff/first/foo/");
});
});
});
7 changes: 5 additions & 2 deletions packages/client/src/createMinimalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ export function createMinimalClient(
throw new Error(`Invalid stack URL: ${baseUrl}${hint}`);
}
}

const processedBaseUrl = new URL(baseUrl);
processedBaseUrl.pathname += processedBaseUrl.pathname.endsWith("/")
? ""
: "/";
const minimalClient: MinimalClient = {
...createSharedClientContext(
baseUrl,
processedBaseUrl.toString(),
tokenProvider,
USER_AGENT,
fetchFn,
Expand Down
31 changes: 31 additions & 0 deletions packages/client/src/object/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function asV2Object(o: any, includeRid?: boolean) {
describe("OsdkObject", () => {
describe("link", () => {
let client: Client;
let customEntryPointClient: Client;

beforeAll(async () => {
apiServer.listen();
Expand All @@ -44,6 +45,11 @@ describe("OsdkObject", () => {
$ontologyRid,
async () => "myAccessToken",
);
customEntryPointClient = createClient(
"https://stack.palantirCustom.com/foo/first/someStuff",
$ontologyRid,
async () => "myAccessToken",
);
});

afterAll(() => {
Expand Down Expand Up @@ -75,6 +81,31 @@ describe("OsdkObject", () => {
// it should have the prototype that we assign at hydration time
expect(Object.keys(employee.$link.lead)).toBeDefined();
});
it("loads an employee with custom client", async () => {
const result = await customEntryPointClient(Employee).where({
employeeId: stubData.employee1.employeeId,
}).fetchPage();

// we should get the employee we requested
const employee = result.data[0];
expect(JSON.stringify(employee)).toBeDefined();
expect(employee).toMatchObject({
"$apiName": "Employee",
"$objectType": "Employee",
"$primaryKey": 50030,
"class": "Red",
"employeeId": 50030,
"employeeStatus": expect.anything(),
"fullName": "John Doe",
"office": "NYC",
"startDate": "2019-01-01",
});

employee.startDate;

// it should have the prototype that we assign at hydration time
expect(Object.keys(employee.$link.lead)).toBeDefined();
});

it("traverses the link from an employee to their lead", async () => {
const result = await client(Employee).where({
Expand Down
15 changes: 12 additions & 3 deletions packages/client/src/objectSet/ObjectSetListenerWebsocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ import { z } from "zod";
import { createMinimalClient } from "../createMinimalClient.js";
import type { Logger } from "../Logger.js";
import type { MinimalClient } from "../MinimalClientContext.js";
import { ObjectSetListenerWebsocket } from "./ObjectSetListenerWebsocket.js";
import {
constructWebsocketUrl,
ObjectSetListenerWebsocket,
} from "./ObjectSetListenerWebsocket.js";

// it needs to be hoisted because its referenced from our mocked WebSocket
// which must be hoisted to work
Expand Down Expand Up @@ -78,7 +81,7 @@ const rootLogger = await vi.hoisted(async (): Promise<Logger> => {
// make local uses of WebSocket typed right
const MockedWebSocket = ImportedWebSocket as unknown as MockedWebSocket;

const STACK = "https://stack.palantir.com";
const STACK = "https://stack.palantirCustom.com/foo/first/someStuff/";

vi.mock("isomorphic-ws", async (importOriginal) => {
const original = await importOriginal<
Expand Down Expand Up @@ -129,7 +132,6 @@ describe("ObjectSetListenerWebsocket", async () => {
async () => "myAccessToken",
{ logger: rootLogger },
);

client = new ObjectSetListenerWebsocket({
...minimalClient,
logger: rootLogger.child({ oslwInst: oslwInst++ }),
Expand Down Expand Up @@ -380,6 +382,13 @@ describe("ObjectSetListenerWebsocket", async () => {
expect(listener.onChange).not.toHaveBeenCalled();
expect(listener.onError).not.toHaveBeenCalled();
});

it("should create url correctly", () => {
expect(constructWebsocketUrl(STACK, "ontologyRid1").toString())
.toEqual(
"wss://stack.palantircustom.com/foo/first/someStuff/api/v2/ontologySubscriptions/ontologies/ontologyRid1/streamSubscriptions",
);
});
});
});
});
Expand Down
19 changes: 16 additions & 3 deletions packages/client/src/objectSet/ObjectSetListenerWebsocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,8 @@ export class ObjectSetListenerWebsocket {
async #ensureWebsocket() {
if (this.#ws == null) {
const { baseUrl, tokenProvider } = this.#client;
const base = new URL(baseUrl);
const url =
`wss://${base.host}/api/v2/ontologySubscriptions/ontologies/${this.#client.ontologyRid}/streamSubscriptions`;
const url = constructWebsocketUrl(baseUrl, this.#client.ontologyRid);

const token = await tokenProvider();

// tokenProvider is async, there could potentially be a race to create the websocket.
Expand Down Expand Up @@ -592,3 +591,17 @@ export class ObjectSetListenerWebsocket {
}
};
}

/** @internal */
export function constructWebsocketUrl(
baseUrl: string,
ontologyRid: string | Promise<string>,
) {
const base = new URL(baseUrl);
const url = new URL(
`api/v2/ontologySubscriptions/ontologies/${ontologyRid}/streamSubscriptions`,
base,
);
url.protocol = url.protocol.replace("https", "wss");
return url;
}
10 changes: 5 additions & 5 deletions packages/foundry-sdk-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
"dependencies": {
"@osdk/api": "workspace:*",
"@osdk/client": "workspace:*",
"@osdk/foundry.core": "2.5.0",
"@osdk/foundry.thirdpartyapplications": "2.5.0",
"@osdk/foundry.core": "2.6.0-beta.0",
"@osdk/foundry.thirdpartyapplications": "2.6.0-beta.0",
"@osdk/generator": "workspace:*",
"@osdk/internal.foundry.core": "2.5.0",
"@osdk/internal.foundry.ontologies": "2.5.0",
"@osdk/internal.foundry.ontologiesv2": "2.5.0",
"@osdk/internal.foundry.core": "2.6.0-beta.0",
"@osdk/internal.foundry.ontologies": "2.6.0-beta.0",
"@osdk/internal.foundry.ontologiesv2": "2.6.0-beta.0",
"@osdk/shared.net": "workspace:*",
"@rollup/plugin-commonjs": "28.0.0",
"@rollup/plugin-node-resolve": "15.3.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/generator-converters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"dependencies": {
"@osdk/api": "workspace:~",
"@osdk/internal.foundry.core": "2.5.0"
"@osdk/internal.foundry.core": "2.6.0-beta.0"
},
"devDependencies": {
"@osdk/monorepo.api-extractor": "workspace:~",
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"dependencies": {
"@osdk/api": "workspace:~",
"@osdk/generator-converters": "workspace:~",
"@osdk/internal.foundry.core": "2.5.0",
"@osdk/internal.foundry.core": "2.6.0-beta.0",
"fast-deep-equal": "^3.1.3",
"fetch-retry": "^6.0.0",
"prettier": "^3.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/maker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"devDependencies": {
"@osdk/client.unstable": "workspace:~",
"@osdk/internal.foundry.core": "2.5.0",
"@osdk/internal.foundry.core": "2.6.0-beta.0",
"@osdk/monorepo.api-extractor": "workspace:~",
"@osdk/monorepo.tsconfig": "workspace:~",
"@osdk/monorepo.tsup": "workspace:~",
Expand Down
1 change: 1 addition & 0 deletions packages/monorepo.cspell/dict.test-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ unstubAllEnvs
interfacers
Claused
geotimeseries
palantircustom
Loading

0 comments on commit 91cfa3a

Please sign in to comment.