Skip to content

Commit

Permalink
feat(pushapi): add method to create a file container (#797)
Browse files Browse the repository at this point in the history
* feat(pushapi): add method to create a file container

* feat(pushapi): handle `useVirtualHostedStyleUrl` when creating a file container
  • Loading branch information
dotboris authored Feb 20, 2024
1 parent 6377d1f commit 4d14d02
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/resources/PushApi/PushApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import API from '../../APICore.js';
import Resource from '../Resource.js';
import {
FileContainer,
FileContainerOptions,
SecurityIdentityAliasModel,
SecurityIdentityBatchConfig,
SecurityIdentityDelete,
Expand All @@ -10,15 +12,19 @@ import {
} from './PushApiInterfaces.js';

export default class PushApi extends Resource {
static baseUrl = `/push/v1/organizations/${API.orgPlaceholder}/providers`;
static baseUrl = `/push/v1/organizations/${API.orgPlaceholder}`;

createFileContainer(options?: FileContainerOptions) {
return this.serverlessApi.post<FileContainer>(this.buildPath(`${PushApi.baseUrl}/files`, options));
}

createOrUpdateSecurityIdentityAlias(
securityProviderId: string,
alias: SecurityIdentityAliasModel,
options?: SecurityIdentityOptions,
) {
return this.serverlessApi.put<void>(
this.buildPath(`${PushApi.baseUrl}/${securityProviderId}/mappings`, options),
this.buildPath(`${PushApi.baseUrl}/providers/${securityProviderId}/mappings`, options),
alias,
);
}
Expand All @@ -29,7 +35,7 @@ export default class PushApi extends Resource {
options?: SecurityIdentityOptions,
) {
return this.serverlessApi.delete<void>(
this.buildPath(`${PushApi.baseUrl}/${securityProviderId}/permissions`, options),
this.buildPath(`${PushApi.baseUrl}/providers/${securityProviderId}/permissions`, options),
{
body: JSON.stringify(securityIdentity),
headers: {'Content-Type': 'application/json'},
Expand All @@ -43,20 +49,20 @@ export default class PushApi extends Resource {
options?: SecurityIdentityOptions,
) {
return this.serverlessApi.put<void>(
this.buildPath(`${PushApi.baseUrl}/${securityProviderId}/permissions`, options),
this.buildPath(`${PushApi.baseUrl}/providers/${securityProviderId}/permissions`, options),
securityIdentity,
);
}

manageSecurityIdentities(securityProviderId: string, batchConfig: SecurityIdentityBatchConfig) {
return this.serverlessApi.put<void>(
this.buildPath(`${PushApi.baseUrl}/${securityProviderId}/permissions/batch`, batchConfig),
this.buildPath(`${PushApi.baseUrl}/providers/${securityProviderId}/permissions/batch`, batchConfig),
);
}

deleteOldSecurityIdentities(securityProviderId: string, batchDelete: SecurityIdentityDeleteOptions) {
return this.serverlessApi.delete<void>(
this.buildPath(`${PushApi.baseUrl}/${securityProviderId}/permissions/olderthan`, batchDelete),
this.buildPath(`${PushApi.baseUrl}/providers/${securityProviderId}/permissions/olderthan`, batchDelete),
);
}
}
15 changes: 15 additions & 0 deletions src/resources/PushApi/PushApiInterfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import {SinglePermissionResult, SinglePermissionIdentityType} from '../Enums.js';

export interface FileContainer {
uploadUri: string;
fileId: string;
requiredHeaders: Record<string, string>;
}

export interface FileContainerOptions {
/**
* Whether to generate the presigned URL using the virtual hosted-style URL.
*
* Example of a virtual hosted-style url: `https://coveo-nprod-customer-data.s3.us-east-1.amazonaws.com/proda/blobstore/mycoveocloudv2organizationg8tp8wu3/b5e8767e-8f0d-4a89-9095-1127915c89c7[...]`
*/
useVirtualHostedStyleUrl?: boolean;
}

export interface SecurityIdentityAliasModel extends SecurityIdentityBase {
mappings: AliasMappings;
}
Expand Down
35 changes: 34 additions & 1 deletion src/resources/PushApi/tests/PushApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import API from '../../../APICore.js';
import {SinglePermissionIdentityType} from '../../Enums.js';
import PushApi from '../PushApi.js';
import {SecurityIdentityAliasModel, SecurityIdentityOptions} from '../PushApiInterfaces.js';
import {FileContainer, SecurityIdentityAliasModel, SecurityIdentityOptions} from '../PushApiInterfaces.js';

jest.mock('../../../APICore.js');

Expand All @@ -27,6 +27,39 @@ describe('PushAPI', () => {
pushApi = new PushApi(api, serverlessApi);
});

describe('createFileContainer', () => {
test.each([
['/push/v1/organizations/{organizationName}/files', undefined],
['/push/v1/organizations/{organizationName}/files', {}],
[
'/push/v1/organizations/{organizationName}/files?useVirtualHostedStyleUrl=true',
{useVirtualHostedStyleUrl: true},
],
[
'/push/v1/organizations/{organizationName}/files?useVirtualHostedStyleUrl=false',
{useVirtualHostedStyleUrl: false},
],
['/push/v1/organizations/{organizationName}/files', {useVirtualHostedStyleUrl: undefined}],
])('should make a POST call to %s when called with %o', async (url, options) => {
serverlessApi.post.mockImplementation(
async (): Promise<FileContainer> => ({
fileId: 'somefileid',
requiredHeaders: {a: 'b', c: 'd'},
uploadUri: 'https://something',
}),
);

const fileContainer = await pushApi.createFileContainer(options);
expect(fileContainer).toEqual({
fileId: 'somefileid',
requiredHeaders: {a: 'b', c: 'd'},
uploadUri: 'https://something',
});
expect(serverlessApi.post).toHaveBeenCalledTimes(1);
expect(serverlessApi.post).toHaveBeenCalledWith(url);
});
});

describe('Security Identity', () => {
describe('createOrUpdateSecurityIdentityAlias', () => {
test.each([
Expand Down

0 comments on commit 4d14d02

Please sign in to comment.