-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[CM] Setup client side content client #150171
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
04060f3
Setup client side content client
Dosant 290c7ea
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine 16e8b91
don’t increase bundle size until needed
Dosant d6b7fbe
Merge branch 'main' into d/2023-02-02-cm-setup-client
kibanamachine c10ee8e
add basic content client tests
Dosant b704196
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine 5de9d5f
add more tests
Dosant df2d6bd
Merge branch 'd/2023-02-02-cm-setup-client' of github.com:Dosant/kiba…
Dosant 765af05
Merge branch 'main' into d/2023-02-02-cm-setup-client
kibanamachine 2ba0f27
clean up
Dosant 2158319
Merge branch 'main' into d/2023-02-02-cm-setup-client
kibanamachine 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 hidden or 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 hidden or 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,72 @@ | ||
| /* | ||
| * 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 { schema, Type } from '@kbn/config-schema'; | ||
|
|
||
| export interface ProcedureSchemas { | ||
| in?: Type<any> | false; | ||
| out?: Type<any> | false; | ||
| } | ||
|
|
||
| export const procedureNames = ['get', 'create'] as const; | ||
|
|
||
| export type ProcedureName = typeof procedureNames[number]; | ||
|
|
||
| // --------------------------------- | ||
| // API | ||
| // --------------------------------- | ||
|
|
||
| // ------- GET -------- | ||
| const getSchemas: ProcedureSchemas = { | ||
| in: schema.object( | ||
| { | ||
| contentType: schema.string(), | ||
| id: schema.string(), | ||
| options: schema.maybe(schema.object({}, { unknowns: 'allow' })), | ||
| }, | ||
| { unknowns: 'forbid' } | ||
| ), | ||
| // --> "out" will be specified by each storage layer | ||
| out: schema.maybe(schema.object({}, { unknowns: 'allow' })), | ||
| }; | ||
|
|
||
| export interface GetIn<Options extends object | undefined = undefined> { | ||
| id: string; | ||
| contentType: string; | ||
| options?: Options; | ||
| } | ||
|
|
||
| // -- Create content | ||
| const createSchemas: ProcedureSchemas = { | ||
| in: schema.object( | ||
| { | ||
| contentType: schema.string(), | ||
| data: schema.object({}, { unknowns: 'allow' }), | ||
| options: schema.maybe(schema.object({}, { unknowns: 'allow' })), | ||
| }, | ||
| { unknowns: 'forbid' } | ||
| ), | ||
| // Here we could enforce that an "id" field is returned | ||
| out: schema.maybe(schema.object({}, { unknowns: 'allow' })), | ||
| }; | ||
|
|
||
| export interface CreateIn< | ||
| T extends string = string, | ||
| Data extends object = Record<string, unknown>, | ||
| Options extends object = any | ||
| > { | ||
| contentType: T; | ||
| data: Data; | ||
| options?: Options; | ||
| } | ||
|
|
||
| export const schemas: { | ||
| [key in ProcedureName]: ProcedureSchemas; | ||
| } = { | ||
| get: getSchemas, | ||
| create: createSchemas, | ||
| }; | ||
62 changes: 62 additions & 0 deletions
62
src/plugins/content_management/public/content_client/content_client.test.ts
This file contains hidden or 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,62 @@ | ||
| /* | ||
| * 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 { lastValueFrom } from 'rxjs'; | ||
| import { takeWhile, toArray } from 'rxjs/operators'; | ||
| import type { RpcClient } from '../rpc_client'; | ||
| import { createRpcClientMock } from '../rpc_client/rpc_client.mock'; | ||
| import { ContentClient } from './content_client'; | ||
| import type { GetIn, CreateIn } from '../../common'; | ||
|
|
||
| let contentClient: ContentClient; | ||
| let rpcClient: jest.Mocked<RpcClient>; | ||
| beforeEach(() => { | ||
| rpcClient = createRpcClientMock(); | ||
| contentClient = new ContentClient(rpcClient); | ||
| }); | ||
|
|
||
| describe('#get', () => { | ||
| it('calls rpcClient.get with input and returns output', async () => { | ||
| const input: GetIn = { id: 'test', contentType: 'testType' }; | ||
| const output = { test: 'test' }; | ||
| rpcClient.get.mockResolvedValueOnce(output); | ||
| expect(await contentClient.get(input)).toEqual(output); | ||
| expect(rpcClient.get).toBeCalledWith(input); | ||
| }); | ||
|
|
||
| it('calls rpcClient.get$ with input and returns output', async () => { | ||
| const input: GetIn = { id: 'test', contentType: 'testType' }; | ||
| const output = { test: 'test' }; | ||
| rpcClient.get.mockResolvedValueOnce(output); | ||
| const get$ = contentClient.get$(input).pipe( | ||
| takeWhile((result) => { | ||
| return result.data == null; | ||
| }, true), | ||
| toArray() | ||
| ); | ||
|
|
||
| const [loadingState, loadedState] = await lastValueFrom(get$); | ||
|
|
||
| expect(loadingState.isLoading).toBe(true); | ||
| expect(loadingState.data).toBeUndefined(); | ||
|
|
||
| expect(loadedState.isLoading).toBe(false); | ||
| expect(loadedState.data).toEqual(output); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#create', () => { | ||
| it('calls rpcClient.create with input and returns output', async () => { | ||
| const input: CreateIn = { contentType: 'testType', data: { foo: 'bar' } }; | ||
| const output = { test: 'test' }; | ||
| rpcClient.create.mockImplementation(() => Promise.resolve(output)); | ||
|
|
||
| expect(await contentClient.create(input)).toEqual(output); | ||
| expect(rpcClient.create).toBeCalledWith(input); | ||
| }); | ||
| }); |
54 changes: 54 additions & 0 deletions
54
src/plugins/content_management/public/content_client/content_client.tsx
This file contains hidden or 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,54 @@ | ||
| /* | ||
| * 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 { QueryClient } from '@tanstack/react-query'; | ||
| import { createQueryObservable } from './query_observable'; | ||
| import type { RpcClient } from '../rpc_client'; | ||
| import type { CreateIn, GetIn } from '../../common'; | ||
|
|
||
| const queryKeyBuilder = { | ||
| all: (type: string) => [type] as const, | ||
| item: (type: string, id: string) => { | ||
| return [...queryKeyBuilder.all(type), id] as const; | ||
| }, | ||
| }; | ||
|
|
||
| const createQueryOptionBuilder = ({ rpcClient }: { rpcClient: RpcClient }) => { | ||
| return { | ||
| get: <I extends GetIn = GetIn, O = unknown>(input: I) => { | ||
| return { | ||
| queryKey: queryKeyBuilder.item(input.contentType, input.id), | ||
| queryFn: () => rpcClient.get<I, O>(input), | ||
| }; | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| export class ContentClient { | ||
| readonly queryClient: QueryClient; | ||
| readonly queryOptionBuilder: ReturnType<typeof createQueryOptionBuilder>; | ||
|
|
||
| constructor(private readonly rpcClient: RpcClient) { | ||
| this.queryClient = new QueryClient(); | ||
| this.queryOptionBuilder = createQueryOptionBuilder({ | ||
| rpcClient: this.rpcClient, | ||
| }); | ||
| } | ||
|
|
||
| get<I extends GetIn = GetIn, O = unknown>(input: I): Promise<O> { | ||
| return this.queryClient.fetchQuery(this.queryOptionBuilder.get(input)); | ||
| } | ||
|
|
||
| get$<I extends GetIn = GetIn, O = unknown>(input: I) { | ||
| return createQueryObservable(this.queryClient, this.queryOptionBuilder.get<I, O>(input)); | ||
| } | ||
|
|
||
| create<I extends CreateIn, O = any>(input: I): Promise<O> { | ||
| return this.rpcClient.create(input); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
src/plugins/content_management/public/content_client/content_client_context.tsx
This file contains hidden or 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,30 @@ | ||
| /* | ||
| * 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 React from 'react'; | ||
| import { QueryClientProvider } from '@tanstack/react-query'; | ||
| import type { ContentClient } from './content_client'; | ||
|
|
||
| const ContentClientContext = React.createContext<ContentClient>(null as unknown as ContentClient); | ||
|
|
||
| export const useContentClient = (): ContentClient => { | ||
| const contentClient = React.useContext(ContentClientContext); | ||
| if (!contentClient) throw new Error('contentClient not found'); | ||
| return contentClient; | ||
| }; | ||
|
|
||
| export const ContentClientProvider: React.FC<{ contentClient: ContentClient }> = ({ | ||
| contentClient, | ||
| children, | ||
| }) => { | ||
| return ( | ||
| <ContentClientContext.Provider value={contentClient}> | ||
| <QueryClientProvider client={contentClient.queryClient}>{children}</QueryClientProvider> | ||
| </ContentClientContext.Provider> | ||
| ); | ||
| }; |
41 changes: 41 additions & 0 deletions
41
src/plugins/content_management/public/content_client/content_client_mutation_hooks.test.tsx
This file contains hidden or 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,41 @@ | ||
| /* | ||
| * 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 React from 'react'; | ||
| import { renderHook } from '@testing-library/react-hooks'; | ||
| import { ContentClientProvider } from './content_client_context'; | ||
| import { ContentClient } from './content_client'; | ||
| import { RpcClient } from '../rpc_client'; | ||
| import { createRpcClientMock } from '../rpc_client/rpc_client.mock'; | ||
| import { useCreateContentMutation } from './content_client_mutation_hooks'; | ||
| import type { CreateIn } from '../../common'; | ||
|
|
||
| let contentClient: ContentClient; | ||
| let rpcClient: jest.Mocked<RpcClient>; | ||
| beforeEach(() => { | ||
| rpcClient = createRpcClientMock(); | ||
| contentClient = new ContentClient(rpcClient); | ||
| }); | ||
|
|
||
| const Wrapper: React.FC = ({ children }) => ( | ||
| <ContentClientProvider contentClient={contentClient}>{children}</ContentClientProvider> | ||
| ); | ||
|
|
||
| describe('useCreateContentMutation', () => { | ||
| test('should call rpcClient.create with input and resolve with output', async () => { | ||
| const input: CreateIn = { contentType: 'testType', data: { foo: 'bar' } }; | ||
| const output = { test: 'test' }; | ||
| rpcClient.create.mockImplementation(() => Promise.resolve(output)); | ||
| const { result, waitFor } = renderHook(() => useCreateContentMutation(), { wrapper: Wrapper }); | ||
| result.current.mutate(input); | ||
|
|
||
| await waitFor(() => result.current.isSuccess); | ||
|
|
||
| expect(result.current.data).toEqual(output); | ||
| }); | ||
| }); |
20 changes: 20 additions & 0 deletions
20
src/plugins/content_management/public/content_client/content_client_mutation_hooks.tsx
This file contains hidden or 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,20 @@ | ||
| /* | ||
| * 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 { useMutation } from '@tanstack/react-query'; | ||
| import { useContentClient } from './content_client_context'; | ||
| import type { CreateIn } from '../../common'; | ||
|
|
||
| export const useCreateContentMutation = <I extends CreateIn = CreateIn, O = unknown>() => { | ||
| const contentClient = useContentClient(); | ||
| return useMutation({ | ||
| mutationFn: (input: I) => { | ||
| return contentClient.create(input); | ||
| }, | ||
| }); | ||
| }; |
38 changes: 38 additions & 0 deletions
38
src/plugins/content_management/public/content_client/content_client_query_hooks.test.tsx
This file contains hidden or 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,38 @@ | ||
| /* | ||
| * 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 React from 'react'; | ||
| import { renderHook } from '@testing-library/react-hooks'; | ||
| import { ContentClientProvider } from './content_client_context'; | ||
| import { ContentClient } from './content_client'; | ||
| import { RpcClient } from '../rpc_client'; | ||
| import { createRpcClientMock } from '../rpc_client/rpc_client.mock'; | ||
| import { useGetContentQuery } from './content_client_query_hooks'; | ||
| import type { GetIn } from '../../common'; | ||
|
|
||
| let contentClient: ContentClient; | ||
| let rpcClient: jest.Mocked<RpcClient>; | ||
| beforeEach(() => { | ||
| rpcClient = createRpcClientMock(); | ||
| contentClient = new ContentClient(rpcClient); | ||
| }); | ||
|
|
||
| const Wrapper: React.FC = ({ children }) => ( | ||
| <ContentClientProvider contentClient={contentClient}>{children}</ContentClientProvider> | ||
| ); | ||
|
|
||
| describe('useGetContentQuery', () => { | ||
| test('should call rpcClient.get with input and resolve with output', async () => { | ||
| const input: GetIn = { id: 'test', contentType: 'testType' }; | ||
| const output = { test: 'test' }; | ||
| rpcClient.get.mockImplementation(() => Promise.resolve(output)); | ||
| const { result, waitFor } = renderHook(() => useGetContentQuery(input), { wrapper: Wrapper }); | ||
| await waitFor(() => result.current.isSuccess); | ||
| expect(result.current.data).toEqual(output); | ||
| }); | ||
| }); |
32 changes: 32 additions & 0 deletions
32
src/plugins/content_management/public/content_client/content_client_query_hooks.tsx
This file contains hidden or 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,32 @@ | ||
| /* | ||
| * 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 { useQuery, QueryObserverOptions } from '@tanstack/react-query'; | ||
| import { useContentClient } from './content_client_context'; | ||
| import type { GetIn } from '../../common'; | ||
|
|
||
| /** | ||
| * Exposed `useQuery` options | ||
| */ | ||
| export type QueryOptions = Pick<QueryObserverOptions, 'enabled'>; | ||
|
|
||
| /** | ||
| * | ||
| * @param input - get content identifier like "id" and "contentType" | ||
| * @param queryOptions - | ||
| */ | ||
| export const useGetContentQuery = <I extends GetIn = GetIn, O = unknown>( | ||
| input: I, | ||
| queryOptions?: QueryOptions | ||
| ) => { | ||
| const contentClient = useContentClient(); | ||
| return useQuery({ | ||
| ...contentClient.queryOptionBuilder.get<I, O>(input), | ||
| ...queryOptions, | ||
| }); | ||
| }; |
12 changes: 12 additions & 0 deletions
12
src/plugins/content_management/public/content_client/index.ts
This file contains hidden or 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,12 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| export { ContentClient } from './content_client'; | ||
| export { ContentClientProvider, useContentClient } from './content_client_context'; | ||
| export { useGetContentQuery } from './content_client_query_hooks'; | ||
| export { useCreateContentMutation } from './content_client_mutation_hooks'; |
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.
This file is copy paste from the server-side POC #148791.
Assuming this won't change much when initial server-side is merged to main. Also won't be a problem if this is changed a lot