Skip to content

Commit 3dbccba

Browse files
authored
Merge pull request #5 from hbmartin/separate-host-and-client-exports
Separate host and client exports
2 parents b06c44c + 8e75d3e commit 3dbccba

22 files changed

Lines changed: 139 additions & 152 deletions

.claude/settings.local.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"Bash(npm run build:*)"
5+
],
6+
"deny": [],
7+
"ask": []
8+
}
9+
}

package.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
"type": "git",
1414
"url": "https://github.com/hbmartin/react-vscode-webview-ipc"
1515
},
16-
"main": "./dist/index.cjs",
17-
"module": "./dist/index.js",
18-
"types": "./dist/index.d.ts",
16+
"typesVersions": {
17+
"*": {
18+
"host": ["./dist/host.d.ts"],
19+
"client": ["./dist/client.d.ts"]
20+
}
21+
},
1922
"exports": {
20-
".": {
21-
"types": "./dist/index.d.ts",
22-
"import": "./dist/index.js",
23-
"require": "./dist/index.cjs"
24-
},
2523
"./host": {
2624
"types": "./dist/host.d.ts",
2725
"import": "./dist/host.js",

src/lib/client.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
export { type WebviewContextValue } from './client/WebviewContext';
1+
// export { type WebviewContextValue } from './client/WebviewContext';
22
export { WebviewProvider } from './client/WebviewProvider';
33
export { createCtxKey, useWebviewApi } from './client/useWebviewApi';
44
export { useVscodeState } from './client/useVscodeState';
55
export { useLogger } from './client/useLogger';
6-
export { isViewApiRequest, isViewApiResponse, isViewApiEvent, type CtxKey } from './types';
7-
export type { ClientCalls, ViewApiResponse, ViewApiError } from './types';
8-
export type { StateReducer, WebviewKey } from './types/ipcReducer';
9-
export type { WebviewLayout } from './types';
6+
export { isViewApiRequest, type CtxKey } from './types';
7+
export type { ClientCalls, HostCalls, ViewApiResponse, ViewApiError } from './types';
8+
export type { WebviewKey } from './types/reducer';
9+
export type { StateReducer, WebviewLayout } from './client/types';

src/lib/client/WebviewContext.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { ClientCalls, HostCalls, VsCodeApi } from '../types';
1+
import type { ClientCalls, HostCalls } from '../types';
2+
import type { VsCodeApi } from './types';
23

34
/**
45
* Context value interface providing type-safe API access
Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
1-
import type { VsCodeApi } from '../types';
2-
import { LogLevel, type ILogger } from './ILogger';
3-
4-
export interface LogMessage {
5-
type: 'log';
6-
level: LogLevel;
7-
message: string;
8-
data?: Record<string, unknown>;
9-
}
10-
11-
export function isLogMessage(value: unknown): value is LogMessage {
12-
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
13-
return false;
14-
}
15-
if (!('type' in value) || value.type !== 'log') {
16-
return false;
17-
}
18-
if (!('level' in value) || typeof value.level !== 'number') {
19-
return false;
20-
}
21-
if (!('message' in value) || typeof value.message !== 'string') {
22-
return false;
23-
}
24-
return true;
25-
}
1+
import { LogLevel, type ILogger, type LogMessage } from '../types';
2+
import type { VsCodeApi } from './types';
263

274
export class WebviewLogger implements ILogger {
285
constructor(

src/lib/client/WebviewProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
type HostCalls,
1010
type RequestContext,
1111
type ViewApiRequest,
12-
type VsCodeApi,
1312
} from '../types';
13+
import { type VsCodeApi } from './types';
1414
import { generateId } from '../utils';
1515
import { DeferredPromise } from './types';
1616
import { TypedContexts } from './useWebviewApi';

src/lib/client/ipcReducer.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { FnKeys } from '../types/reducer';
2+
3+
export function isFnKey<T extends object>(
4+
prop: string | symbol | number,
5+
obj: T
6+
): prop is FnKeys<T> {
7+
return (
8+
Object.prototype.hasOwnProperty.call(obj, prop) && typeof obj[prop as keyof T] === 'function'
9+
);
10+
}

src/lib/client/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { FnKeys, Patches } from '../types/reducer';
2+
13
/**
24
* Deferred promise for handling async responses with timeout management
35
*/
@@ -42,3 +44,16 @@ export class DeferredPromise<T> {
4244
this.settled = true;
4345
}
4446
}
47+
48+
export type WebviewLayout = 'sidebar' | 'panel';
49+
// VS Code webview API
50+
51+
export interface VsCodeApi {
52+
postMessage(message: unknown): Thenable<boolean>;
53+
getState(): unknown;
54+
setState(state: unknown): void;
55+
}
56+
57+
export type StateReducer<S, A> = {
58+
[Key in FnKeys<A>]: (prevState: S, patch: Patches<A>[Key]) => S;
59+
};

src/lib/client/useLogger.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable no-console */
22
import { useMemo } from 'react';
3-
import type { VsCodeApi } from '../types';
4-
import { WebviewLogger } from '../host/WebviewLogger';
5-
import type { ILogger } from '../host/ILogger';
3+
import type { ILogger } from '../types';
4+
import { WebviewLogger } from './WebviewLogger';
5+
import type { VsCodeApi } from './types';
66

77
/**
88
* React hook to get a logger instance for use in webview components.

src/lib/client/useVscodeState.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { useCallback, useEffect, useMemo, useState } from 'react';
2-
import type { VsCodeApi } from '../types';
3-
import {
4-
ACT,
5-
type Patch,
6-
PATCH,
7-
type Action,
8-
type WebviewKey,
9-
type StateReducer,
10-
isFnKey,
11-
} from '../types/ipcReducer';
2+
import { ACT, type Patch, PATCH, type Action, type WebviewKey } from '../types/reducer';
3+
import { isFnKey } from './ipcReducer';
4+
import type { StateReducer, VsCodeApi } from './types';
125

136
type PostAction<A extends object> = Pick<Action<A>, 'key' | 'params'>;
147

0 commit comments

Comments
 (0)