Skip to content

Commit

Permalink
feat: add get/put partials to PromptsManager
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-hamilton committed Jan 24, 2024
1 parent 04b109b commit 84a9744
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/management/__generated/managers/prompts-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type {
PromptsSettingsUpdate,
GetCustomTextByLanguageRequest,
PutCustomTextByLanguageRequest,
GetTeplatePartialsByPromptRequest,
PutTeplatePartialsByPromptRequest,
} from '../models/index.js';

const { BaseAPI } = runtime;
Expand Down Expand Up @@ -38,6 +40,32 @@ export class PromptsManager extends BaseAPI {
return runtime.JSONApiResponse.fromResponse<any>(response);
}

/**
* Retrieve template partials for a specific prompt.
* Get template partials for a prompt
*
* @throws {RequiredError}
*/
async getTemplatePartialsByPrompt(
requestParameters: GetTeplatePartialsByPromptRequest,
initOverrides?: InitOverride
): Promise<ApiResponse<{ [key: string]: any }>> {
runtime.validateRequiredRequestParams(requestParameters, ['prompt']);

const response = await this.request(
{
path: `/prompts/{prompt}/partials`.replace(
'{prompt}',
encodeURIComponent(String(requestParameters.prompt))
),
method: 'GET',
},
initOverrides
);

return runtime.JSONApiResponse.fromResponse(response);
}

/**
* Retrieve prompts settings.
* Get prompts settings
Expand Down Expand Up @@ -114,4 +142,37 @@ export class PromptsManager extends BaseAPI {

return runtime.VoidApiResponse.fromResponse(response);
}

/**
* Set template partials for a specific prompt. Existing partials will be overwritten.
* Set template partials for a specific prompt
*
* @throws {RequiredError}
*/
async updateTemplatePartialsByPrompt(
requestParameters: PutTeplatePartialsByPromptRequest,
bodyParameters: { [key: string]: any },
initOverrides?: InitOverride
): Promise<ApiResponse<{ [key: string]: any }>> {
runtime.validateRequiredRequestParams(requestParameters, ['prompt']);

const headerParameters: runtime.HTTPHeaders = {};

headerParameters['Content-Type'] = 'application/json';

const response = await this.request(
{
path: `/prompts/{prompt}/partials`.replace(
'{prompt}',
encodeURIComponent(String(requestParameters.prompt))
),
method: 'PUT',
headers: headerParameters,
body: bodyParameters,
},
initOverrides
);

return runtime.JSONApiResponse.fromResponse(response);
}
}
54 changes: 54 additions & 0 deletions src/management/__generated/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13602,6 +13602,60 @@ export const GetCustomTextByLanguagePromptEnum = {
export type GetCustomTextByLanguagePromptEnum =
(typeof GetCustomTextByLanguagePromptEnum)[keyof typeof GetCustomTextByLanguagePromptEnum];

/**
*
*/
export interface GetTeplatePartialsByPromptRequest {
/**
* Name of the prompt.
*
*/
prompt: GetTemplatePartialsByPromptEnum;
}

/**
*
*/
export interface PutTeplatePartialsByPromptRequest {
/**
* Name of the prompt.
*
*/
prompt: PutTemplatePartialsByPromptEnum;
}

/**
*
*/
export const GetTemplatePartialsByPromptEnum = {
login: 'login',
login_id: 'login-id',
login_password: 'login-password',
login_passwordless: 'login-passwordless',
signup: 'signup',
signup_id: 'signup-id',
signup_password: 'signup-password',
};

export type GetTemplatePartialsByPromptEnum =
(typeof GetTemplatePartialsByPromptEnum)[keyof typeof GetTemplatePartialsByPromptEnum];

/**
*
*/
export const PutTemplatePartialsByPromptEnum = {
login: 'login',
login_id: 'login-id',
login_password: 'login-password',
login_passwordless: 'login-passwordless',
signup: 'signup',
signup_id: 'signup-id',
signup_password: 'signup-password',
};

export type PutTemplatePartialsByPromptEnum =
(typeof PutTemplatePartialsByPromptEnum)[keyof typeof PutTemplatePartialsByPromptEnum];

/**
*
*/
Expand Down
160 changes: 160 additions & 0 deletions test/management/prompts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
PromptsManager,
PromptsSettingsUniversalLoginExperienceEnum,
PromptsSettingsUpdateUniversalLoginExperienceEnum,
GetTemplatePartialsByPromptEnum,
PutTemplatePartialsByPromptEnum,
ManagementClient,
RequiredError,
} from '../../src/index.js';
Expand Down Expand Up @@ -310,4 +312,162 @@ describe('PromptsManager', () => {
});
});
});

describe('#getTemplatePartialsByPrompt', () => {
let request: nock.Scope;
const params = {
prompt: GetTemplatePartialsByPromptEnum.login,
};

const partial = { login: { 'form-content-start': '<div>TEST</div>' } };

beforeEach(() => {
request = nock(API_URL).get('/prompts/login/partials').reply(200, partial);
});

it('should validate empty prompt parameter', async () => {
await expect(prompts.getTemplatePartialsByPrompt({} as any)).rejects.toThrowError(
RequiredError
);
});

it('should return a promise if no callback is given', (done) => {
prompts
.getTemplatePartialsByPrompt(params)
.then(done.bind(null, null))
.catch(done.bind(null, null));
});

it('should pass any errors to the promise catch handler', (done) => {
nock.cleanAll();

nock(API_URL).get('/prompts/login/partials').reply(500, {});

prompts.getTemplatePartialsByPrompt(params).catch((err) => {
expect(err).toBeDefined();

done();
});
});

it('should perform a GET request to /api/v2/prompts/login/partials', (done) => {
prompts.getTemplatePartialsByPrompt(params).then(() => {
expect(request.isDone()).toBe(true);
done();
});
});

it('should return the value returned from the Management API', (done) => {
prompts.getTemplatePartialsByPrompt(params).then((res) => {
expect(res.data).toEqual(partial);
done();
});
});

it('should include the token in the Authorization header', (done) => {
nock.cleanAll();

const request = nock(API_URL)
.get('/prompts/login/partials')
.matchHeader('Authorization', `Bearer ${token}`)
.reply(200, {});

prompts.getTemplatePartialsByPrompt(params).then(() => {
expect(request.isDone()).toBe(true);

done();
});
});
});

describe('#updateTemplatePartialsByPrompt', () => {
let request: nock.Scope;
const prompt = PutTemplatePartialsByPromptEnum.login;
const partial = { login: { 'form-content-start': '<div>TESTING</div>' } };

beforeEach(() => {
request = nock(API_URL).put('/prompts/login/partials').reply(200, partial);
});

it('should validate empty prompt parameter', async () => {
await expect(prompts.updateTemplatePartialsByPrompt({} as any, {})).rejects.toThrowError(
RequiredError
);
});

it('should return a promise if no callback is given', (done) => {
prompts
.updateTemplatePartialsByPrompt({ prompt }, partial)
.then(done.bind(null, null))
.catch(done.bind(null, null));
});

it('should pass any errors to the promise catch handler', (done) => {
nock.cleanAll();

nock(API_URL).put('/prompts/login/partials').reply(500, {});

prompts.updateTemplatePartialsByPrompt({ prompt }, partial).catch((err) => {
expect(err).toBeDefined();

done();
});
});

it('should perform a PUT request to /api/v2/prompts/login/partials', (done) => {
prompts
.updateTemplatePartialsByPrompt({ prompt }, partial)
.then(() => {
expect(request.isDone()).toBe(true);
done();
})
.catch((e) => {
console.error(e);
});
});

it('should return the value returned from the Management API', (done) => {
prompts
.updateTemplatePartialsByPrompt({ prompt }, partial)
.then((res) => {
expect(res.data).toEqual(partial);
done();
})
.catch((e) => {
console.error(e);
});
});

it('should include the token in the Authorization header', (done) => {
nock.cleanAll();

const request = nock(API_URL)
.put('/prompts/login/partials')
.matchHeader('Authorization', `Bearer ${token}`)
.reply(200, {});

prompts.updateTemplatePartialsByPrompt({ prompt }, partial).then(() => {
expect(request.isDone()).toBe(true);

done();
});
});

it('should send the payload to the body', (done) => {
nock.cleanAll();

const request = nock(API_URL)
.put(
'/prompts/login/partials',
(body) => body?.login?.['form-content-start'] === '<div>TESTING</div>'
)
.reply(200, {});

prompts.updateTemplatePartialsByPrompt({ prompt }, partial).then(() => {
expect(request.isDone()).toBe(true);

done();
});
});
});
});

0 comments on commit 84a9744

Please sign in to comment.