support createPaste

Summary:
Added support for `createPaste` in Sandy plugins

Nice minimalistic example of how to expose a Flipper api to Sandy.

Note that some indirection could be removed by having an interface that is shared directly between `BasePluginClient` and `FlipperLib` (e.g. `PublicFlipperLib`). In contrast to `addMenuEntries` from the previous diff, `createPaste` is basically exposed verbatim to Sandy without additional wrapping, so those cases could be made simpler. Maybe will do that later.

Reviewed By: passy

Differential Revision: D22815873

fbshipit-source-id: e6d0773a35341edfe5de0898317eaadf88de79d0
This commit is contained in:
Michel Weststrate
2020-08-04 07:44:56 -07:00
committed by Facebook GitHub Bot
parent 9c202a4a10
commit 7c6065889d
6 changed files with 37 additions and 2 deletions

View File

@@ -10,6 +10,7 @@
import type {FlipperLib} from 'flipper-plugin'; import type {FlipperLib} from 'flipper-plugin';
import type {Logger} from '../fb-interfaces/Logger'; import type {Logger} from '../fb-interfaces/Logger';
import type {Store} from '../reducers'; import type {Store} from '../reducers';
import createPaste from '../fb-stubs/createPaste';
let flipperLibInstance: FlipperLib | undefined; let flipperLibInstance: FlipperLib | undefined;
@@ -23,6 +24,7 @@ export function initializeFlipperLibImplementation(
enableMenuEntries(entries) { enableMenuEntries(entries) {
addSandyPluginEntries(entries); addSandyPluginEntries(entries);
}, },
createPaste,
}; };
} }

View File

@@ -328,3 +328,21 @@ test('plugins can register menu entries', async () => {
`"No menu entry found with action: Non Existing"`, `"No menu entry found with action: Non Existing"`,
); );
}); });
test('plugins can create pastes', async () => {
const plugin = TestUtils.startPlugin({
plugin(client: PluginClient) {
return {
doIt() {
client.createPaste('test');
},
};
},
Component() {
return null;
},
});
plugin.instance.doIt();
expect(plugin.flipperLib.createPaste).toBeCalledWith('test');
});

View File

@@ -14,4 +14,5 @@ import {NormalizedMenuEntry} from './MenuEntry';
*/ */
export interface FlipperLib { export interface FlipperLib {
enableMenuEntries(menuEntries: NormalizedMenuEntry[]): void; enableMenuEntries(menuEntries: NormalizedMenuEntry[]): void;
createPaste(input: string): Promise<string | undefined>;
} }

View File

@@ -38,6 +38,12 @@ export interface BasePluginClient {
* Register menu entries in the Flipper toolbar * Register menu entries in the Flipper toolbar
*/ */
addMenuEntry(...entry: MenuEntry[]): void; addMenuEntry(...entry: MenuEntry[]): void;
/**
* Creates a Paste (similar to a Github Gist).
* Facebook only function. Resolves to undefined if creating a paste failed.
*/
createPaste(input: string): Promise<string | undefined>;
} }
let currentPluginInstance: BasePluginInstance | undefined = undefined; let currentPluginInstance: BasePluginInstance | undefined = undefined;
@@ -123,6 +129,7 @@ export abstract class BasePluginInstance {
this.menuEntries.push(normalizeMenuEntry(entry)); this.menuEntries.push(normalizeMenuEntry(entry));
} }
}, },
createPaste: this.flipperLib.createPaste,
}; };
} }

View File

@@ -324,6 +324,7 @@ export function renderDevicePlugin<Module extends FlipperDevicePluginModule>(
export function createMockFlipperLib(): FlipperLib { export function createMockFlipperLib(): FlipperLib {
return { return {
enableMenuEntries: jest.fn(), enableMenuEntries: jest.fn(),
createPaste: jest.fn(),
}; };
} }

View File

@@ -59,8 +59,14 @@ export function plugin(client: FlipperClient<Events, {}>) {
}, },
{ {
action: 'createPaste', action: 'createPaste',
handler: () => { handler: async () => {
console.log('creating paste'); const selection = selectedID.get();
if (selection) {
const url = await client.createPaste(
JSON.stringify(rows.get()[selection], null, 2),
);
alert(url); // TODO: use notifications T69990351
}
}, },
}, },
); );