Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Content Management] Server side client #175968

Merged
merged 26 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ed9a871
plan
sebelga Jan 18, 2024
71f022b
Initial commit
sebelga Jan 30, 2024
bfd599c
Refactor Core API
sebelga Jan 30, 2024
a7521e6
Add core contentClient tests
sebelga Jan 31, 2024
869f585
Add comments
sebelga Jan 31, 2024
a853fb0
Fix core tests
sebelga Jan 31, 2024
7ed9569
Enforce ContentCrud instance to be passed to ContentClient
sebelga Jan 31, 2024
2f87fa6
Fix core tests
sebelga Jan 31, 2024
24878d1
Refactor core api
sebelga Jan 31, 2024
20c01b6
Require also the KibanaRequest to be passed
sebelga Jan 31, 2024
d96832d
Correctly type content returned
sebelga Jan 31, 2024
35aa15b
Add create, update and delete methods
sebelga Jan 31, 2024
5de4d2e
Add bulkGet method
sebelga Jan 31, 2024
74dfdc9
Add meta prop
sebelga Jan 31, 2024
8132f92
Add search method
sebelga Jan 31, 2024
b87acb0
Expose mSearch function from core Api
sebelga Jan 31, 2024
b78126a
Update RPC procedures to use the clients
sebelga Jan 31, 2024
d783d2c
Update comments
sebelga Jan 31, 2024
9046b00
Fix jest test & TS issues
sebelga Jan 31, 2024
73a0d61
Small refactor
sebelga Feb 1, 2024
d81a968
Automatically add the latest version on msearch
sebelga Feb 1, 2024
87834ac
Add test for register response to return a ContentClient
sebelga Feb 1, 2024
cd90d60
Merge remote-tracking branch 'upstream/main' into content-management/…
sebelga Feb 1, 2024
7fe6871
Add code comments
sebelga Feb 1, 2024
6b247ae
Merge branch 'main' into content-management/server-client
sebelga Feb 2, 2024
fe950a5
Merge branch 'main' into content-management/server-client
sebelga Feb 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,20 @@ const validateServiceDefinitions = (definitions: ServiceDefinitionVersioned) =>
* ```ts
* From this
* {
* // Service definition version 1
* 1: {
* get: {
* in: {
* options: { up: () => {} } // 1
* options: { up: () => {} }
* }
* },
* ...
* },
* // Service definition version 2
* 2: {
* get: {
* in: {
* options: { up: () => {} } // 2
* options: { up: () => {} }
* }
* },
* }
Expand Down
1 change: 1 addition & 0 deletions src/plugins/content_management/common/rpc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface ProcedureSchemas {
export type ItemResult<T = unknown, M = void> = M extends void
? {
item: T;
meta?: never;
}
: {
item: T;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { ContentCrud } from '../core/crud';
import { EventBus } from '../core/event_bus';
import { createMemoryStorage, type FooContent } from '../core/mocks';
import { ContentClient } from './content_client';

describe('ContentClient', () => {
const setup = ({
contentTypeId = 'foo',
}: {
contentTypeId?: string;
} = {}) => {
const storage = createMemoryStorage();
const eventBus = new EventBus();
const crudInstance = new ContentCrud<FooContent>(contentTypeId, storage, { eventBus });

const contentClient = ContentClient.create(contentTypeId, {
crudInstance,
storageContext: {} as any,
});

return { contentClient };
};

describe('instance', () => {
test('should throw an Error if instantiate using constructor', () => {
const expectToThrow = () => {
new ContentClient(Symbol('foo'), 'foo', {} as any);
};
expect(expectToThrow).toThrowError('Use ContentClient.create() instead');
});

test('should have contentTypeId', () => {
const { contentClient } = setup({ contentTypeId: 'hellooo' });
expect(contentClient.contentTypeId).toBe('hellooo');
});

test('should throw if crudInstance is not an instance of ContentCrud', () => {
const expectToThrow = () => {
ContentClient.create('foo', {
crudInstance: {} as any,
storageContext: {} as any,
});
};
// With this test and runtime check we can rely on all the existing tests of the Content Crud.
// e.g. the tests about events being dispatched, etc.
expect(expectToThrow).toThrowError('Crud instance missing or not an instance of ContentCrud');
});
});

describe('Crud', () => {
describe('create()', () => {
test('should create an item', async () => {
const { contentClient } = setup();
const itemCreated = await contentClient.create({ foo: 'bar' });
const { id } = itemCreated.result.item;
const res = await contentClient.get(id);
expect(res.result.item).toEqual({ foo: 'bar', id });
});

test('should pass the options to the storage', async () => {
const { contentClient } = setup();

const options = { forwardInResponse: { option1: 'foo' } };
const res = await contentClient.create({ field1: 123 }, options);
expect(res.result.item).toEqual({
field1: 123,
id: expect.any(String),
options: { option1: 'foo' }, // the options have correctly been passed to the storage
});
});
});

describe('get()', () => {
// Note: we test the client get() method in multiple other tests for
// the "create()" and "update()" methods, no need for extended tests here.
test('should return undefined if no item was found', async () => {
const { contentClient } = setup();
const res = await contentClient.get('hello');
expect(res.result.item).toBeUndefined();
});

test('should pass the options to the storage', async () => {
const { contentClient } = setup();

const options = { forwardInResponse: { foo: 'bar' } };
const res = await contentClient.get('hello', options);

expect(res.result.item).toEqual({
// the options have correctly been passed to the storage
options: { foo: 'bar' },
});
});
});

describe('bulkGet()', () => {
test('should return multiple items', async () => {
const { contentClient } = setup();

const item1 = await contentClient.create({ name: 'item1' });
const item2 = await contentClient.create({ name: 'item2' });
const ids = [item1.result.item.id, item2.result.item.id];

const res = await contentClient.bulkGet(ids);
expect(res.result.hits).toEqual([
{
item: {
name: 'item1',
id: expect.any(String),
},
},
{
item: {
name: 'item2',
id: expect.any(String),
},
},
]);
});

test('should pass the options to the storage', async () => {
const { contentClient } = setup();

const item1 = await contentClient.create({ name: 'item1' });
const item2 = await contentClient.create({ name: 'item2' });
const ids = [item1.result.item.id, item2.result.item.id];

const options = { forwardInResponse: { foo: 'bar' } };
const res = await contentClient.bulkGet(ids, options);

expect(res.result.hits).toEqual([
{
item: {
name: 'item1',
id: expect.any(String),
options: { foo: 'bar' }, // the options have correctly been passed to the storage
},
},
{
item: {
name: 'item2',
id: expect.any(String),
options: { foo: 'bar' }, // the options have correctly been passed to the storage
},
},
]);
});
});

describe('update()', () => {
test('should update an item', async () => {
const { contentClient } = setup();
const itemCreated = await contentClient.create({ foo: 'bar' });
const { id } = itemCreated.result.item;

await contentClient.update(id, { foo: 'changed' });

const res = await contentClient.get(id);
expect(res.result.item).toEqual({ foo: 'changed', id });
});

test('should pass the options to the storage', async () => {
const { contentClient } = setup();
const itemCreated = await contentClient.create({ field1: 'bar' });
const { id } = itemCreated.result.item;

const options = { forwardInResponse: { option1: 'foo' } };
const res = await contentClient.update(id, { field1: 'changed' }, options);

expect(res.result.item).toEqual({
field1: 'changed',
id,
options: { option1: 'foo' }, // the options have correctly been passed to the storage
});
});
});

describe('delete()', () => {
test('should delete an item', async () => {
const { contentClient } = setup();
const itemCreated = await contentClient.create({ foo: 'bar' });
const { id } = itemCreated.result.item;

{
const res = await contentClient.get(id);
expect(res.result.item).not.toBeUndefined();
}

await contentClient.delete(id);

{
const res = await contentClient.get(id);
expect(res.result.item).toBeUndefined();
}
});

test('should pass the options to the storage', async () => {
const { contentClient } = setup();
const itemCreated = await contentClient.create({ field1: 'bar' });
const { id } = itemCreated.result.item;

const options = { forwardInResponse: { option1: 'foo' } };

const res = await contentClient.delete(id, options);

expect(res.result).toEqual({
success: true,
options: { option1: 'foo' }, // the options have correctly been passed to the storage
});
});
});

describe('search()', () => {
test('should find an item', async () => {
const { contentClient } = setup();

await contentClient.create({ title: 'hello' });

const res = await contentClient.search({ text: 'hello' });

expect(res.result).toEqual({
hits: [
{
id: expect.any(String),
title: 'hello',
},
],
pagination: {
cursor: '',
total: 1,
},
});
});

test('should pass the options to the storage', async () => {
const { contentClient } = setup();
await contentClient.create({ title: 'hello' });

const options = { forwardInResponse: { option1: 'foo' } };
const res = await contentClient.search({ text: 'hello' }, options);

expect(res.result).toEqual({
hits: [
{
id: expect.any(String),
title: 'hello',
options: { option1: 'foo' }, // the options have correctly been passed to the storage
},
],
pagination: {
cursor: '',
total: 1,
},
});
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import type { StorageContext } from '../core';
import { ContentCrud } from '../core/crud';
import type { IContentClient } from './types';

interface Context<T = unknown> {
crudInstance: ContentCrud<T>;
storageContext: StorageContext;
}

const secretToken = Symbol('secretToken');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: this never gets stored. I had to check because that could pose an issue if we ever handle a token that was created in a separate Kibana instance


export class ContentClient<T = unknown> implements IContentClient<T> {
static create<T = unknown>(contentTypeId: string, ctx: Context<T>): IContentClient<T> {
return new ContentClient<T>(secretToken, contentTypeId, ctx);
}

constructor(token: symbol, public contentTypeId: string, private readonly ctx: Context<T>) {
if (token !== secretToken) {
throw new Error('Use ContentClient.create() instead');
}

if (ctx.crudInstance instanceof ContentCrud === false) {
throw new Error('Crud instance missing or not an instance of ContentCrud');
}
}

get(id: string, options: object) {
return this.ctx.crudInstance.get(this.ctx.storageContext, id, options);
}

bulkGet(ids: string[], options: object) {
return this.ctx.crudInstance.bulkGet(this.ctx.storageContext, ids, options);
}

create(data: object, options?: object) {
return this.ctx.crudInstance.create(this.ctx.storageContext, data, options);
}

update(id: string, data: object, options?: object) {
return this.ctx.crudInstance.update(this.ctx.storageContext, id, data, options);
}

delete(id: string, options?: object) {
return this.ctx.crudInstance.delete(this.ctx.storageContext, id, options);
}

search(query: object, options?: object) {
return this.ctx.crudInstance.search(this.ctx.storageContext, query, options);
}
}
Loading