Skip to content

Commit

Permalink
feat(Layout): add executeScript to layout
Browse files Browse the repository at this point in the history
  • Loading branch information
lart2150 committed May 31, 2024
1 parent d67d967 commit 5c729a4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export type GetParams<U extends GenericPortalData = GenericPortalData> = ScriptP

export type Sort<T extends FieldData = FieldData> = {
fieldName : keyof T;
sortOrder : 'ascend' | 'descend' | string;
sortOrder : 'ascend' | 'descend';
};

export type ListParams<
Expand Down Expand Up @@ -111,6 +111,8 @@ export type File = {
buffer : Buffer;
};

export type ExecuteScriptResponse = Pick<ScriptResponse, 'scriptResult' | 'scriptError'>;

export default class Layout<T extends FieldData = FieldData, U extends GenericPortalData = GenericPortalData> {
public constructor(private readonly layout : string, private readonly client : Client) {
}
Expand Down Expand Up @@ -274,6 +276,24 @@ export default class Layout<T extends FieldData = FieldData, U extends GenericPo
}
}

/**
* The script parameter will be in the query parameter so do not send sensitive information in it.
*/
public async executeScript(
scriptName : string,
scriptParam ?: string,
) : Promise<ExecuteScriptResponse> {
const searchParams = new URLSearchParams();

if (scriptParam) {
searchParams.set('script.param', scriptParam);
}

return await this.client.request<ExecuteScriptResponse>(
`layouts/${this.layout}/script/${encodeURI(scriptName)}?${searchParams.toString()}`
);
}

private addPortalRangesToRequest(
portalRanges : PortalRanges<U>,
request : Record<string, unknown> | URLSearchParams
Expand Down
22 changes: 22 additions & 0 deletions test/Layout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,26 @@ describe('Layout', () => {
expect(response).toEqual({data: []});
});
});

describe('executeScript', () => {
it('should send a execute script call', async () => {
const expectedResponse = {scriptResult: 'bar', scriptError: '0'};

clientMock.request.withArgs('layouts/foo/script/testscript?')
.returns(Promise.resolve(expectedResponse));

const response = await layout.executeScript('testscript');
expect(response).toBe(expectedResponse);
});

it('should send a execute script call with param', async () => {
const expectedResponse = {scriptError: '0'};

clientMock.request.withArgs('layouts/foo/script/test%20%7C%20script?script.param=%3Ftest%2B%3Dparam')
.returns(Promise.resolve(expectedResponse));

const response = await layout.executeScript('test | script', '?test+=param');
expect(response).toBe(expectedResponse);
});
});
});

0 comments on commit 5c729a4

Please sign in to comment.