-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
sebelga
merged 26 commits into
elastic:main
from
sebelga:content-management/server-client
Feb 5, 2024
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
ed9a871
plan
sebelga 71f022b
Initial commit
sebelga bfd599c
Refactor Core API
sebelga a7521e6
Add core contentClient tests
sebelga 869f585
Add comments
sebelga a853fb0
Fix core tests
sebelga 7ed9569
Enforce ContentCrud instance to be passed to ContentClient
sebelga 2f87fa6
Fix core tests
sebelga 24878d1
Refactor core api
sebelga 20c01b6
Require also the KibanaRequest to be passed
sebelga d96832d
Correctly type content returned
sebelga 35aa15b
Add create, update and delete methods
sebelga 5de4d2e
Add bulkGet method
sebelga 74dfdc9
Add meta prop
sebelga 8132f92
Add search method
sebelga b87acb0
Expose mSearch function from core Api
sebelga b78126a
Update RPC procedures to use the clients
sebelga d783d2c
Update comments
sebelga 9046b00
Fix jest test & TS issues
sebelga 73a0d61
Small refactor
sebelga d81a968
Automatically add the latest version on msearch
sebelga 87834ac
Add test for register response to return a ContentClient
sebelga cd90d60
Merge remote-tracking branch 'upstream/main' into content-management/…
sebelga 7fe6871
Add code comments
sebelga 6b247ae
Merge branch 'main' into content-management/server-client
sebelga fe950a5
Merge branch 'main' into content-management/server-client
sebelga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
264 changes: 264 additions & 0 deletions
264
src/plugins/content_management/server/content_client/content_client.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
57 changes: 57 additions & 0 deletions
57
src/plugins/content_management/server/content_client/content_client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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