Skip to content

Commit 4746a54

Browse files
committed
update: move org_uid to headers
To ensure that the getAllStacks only receive the list of stacks that the user is currently a member of, we updated the url to /stacks. Now, this route accepts the organization uid through headers. Also, this route now uses getStacks instead of getAllStacks as that route was redundant. But, the event adds the api key by default. So, we have added another key to skip the api key in the headers.
1 parent 3fb529e commit 4746a54

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

__test__/stack.test.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,58 @@ describe("Stack", () => {
5252
});
5353
});
5454

55-
it("getAllStacks", (done) => {
55+
it("getAllStacks should get called with default org Uid when not provided", (done) => {
5656
stack.getAllStacks().then((data) => {
5757
expect(data.length).toBe(0);
5858
expect(connection.sendToParent).toHaveBeenCalledWith(
5959
"stackQuery",
60-
{ org_uid: getStack().org_uid, action: "getAllStacks" }
60+
{
61+
headers: { organization_uid: getStack().org_uid },
62+
action: "getStacks",
63+
params: {},
64+
skip_api_key: true,
65+
}
66+
);
67+
done();
68+
});
69+
});
70+
71+
it("getAllStacks should get called with provided org Uid", (done) => {
72+
let orgUid = "some-org-uid";
73+
stack.getAllStacks({ orgUid }).then((data) => {
74+
expect(data.length).toBe(0);
75+
expect(connection.sendToParent).toHaveBeenCalledWith(
76+
"stackQuery",
77+
{
78+
headers: { organization_uid: orgUid },
79+
action: "getStacks",
80+
params: {},
81+
skip_api_key: true,
82+
}
83+
);
84+
done();
85+
});
86+
});
87+
88+
it("getAllStacks should throw error when uid is not string", async () => {
89+
let orgUid = 123 as any;
90+
await expect(stack.getAllStacks({ orgUid })).rejects.toThrowError(
91+
"orgUid must be a string"
92+
);
93+
});
94+
95+
it("getAllStacks should send query params", (done) => {
96+
let params = { sample: "parameter" };
97+
stack.getAllStacks({ params }).then((data) => {
98+
expect(data.length).toBe(0);
99+
expect(connection.sendToParent).toHaveBeenCalledWith(
100+
"stackQuery",
101+
{
102+
headers: { organization_uid: getStack().org_uid },
103+
action: "getStacks",
104+
params,
105+
skip_api_key: true,
106+
}
61107
);
62108
done();
63109
});

src/stack/index.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Asset from './api/asset/index';
22
import ContentType from './api/content-type/index';
33
import { onData, onError } from "../utils";
4-
import { BranchDetail, StackAdditionalData, StackDetail, StackSearchQuery } from '../types/stack.types';
4+
import { BranchDetail, GetAllStacksOptions, StackAdditionalData, StackDetail, StackSearchQuery } from '../types/stack.types';
55

66

77
/**
@@ -66,10 +66,24 @@ class Stack {
6666

6767
/**
6868
* This method returns all the stacks in the current organization.
69+
* @param query asks for organization UID and query params to get all stacks
6970
* @returns Stacks within current organization
7071
*/
71-
getAllStacks(): Promise<StackDetail[]> {
72-
const options = { action: "getAllStacks", org_uid: this._data.org_uid };
72+
async getAllStacks({orgUid = "", params = {}}: GetAllStacksOptions = {}): Promise<StackDetail[]> {
73+
74+
console.log("getAllStacks", orgUid, typeof orgUid);
75+
76+
// validation
77+
if (typeof orgUid !== 'string') {
78+
throw new TypeError('orgUid must be a string');
79+
}
80+
81+
const options = {
82+
action: "getStacks",
83+
headers: { organization_uid: orgUid || this._data.org_uid },
84+
skip_api_key: true,
85+
params
86+
};
7387
return this._connection
7488
.sendToParent("stackQuery", options)
7589
.then(onData)

src/types/stack.types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,8 @@ export declare interface StackSearchQuery {
103103
save_recent_search?: boolean;
104104
desc?: string;
105105
}
106+
107+
export declare interface GetAllStacksOptions {
108+
orgUid?: string;
109+
params?: AnyObject;
110+
}

0 commit comments

Comments
 (0)