@palantir/pack.app.react 0.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-lint.log +4 -0
- package/.turbo/turbo-transpileBrowser.log +5 -0
- package/.turbo/turbo-transpileCjs.log +5 -0
- package/.turbo/turbo-transpileEsm.log +5 -0
- package/.turbo/turbo-transpileTypes.log +5 -0
- package/.turbo/turbo-typecheck.log +4 -0
- package/LICENSE.txt +13 -0
- package/README.md +36 -0
- package/build/browser/index.js +56 -0
- package/build/browser/index.js.map +1 -0
- package/build/cjs/index.cjs +60 -0
- package/build/cjs/index.cjs.map +1 -0
- package/build/cjs/index.d.cts +30 -0
- package/build/esm/index.js +56 -0
- package/build/esm/index.js.map +1 -0
- package/build/types/__tests__/usePackApp.test.d.ts +1 -0
- package/build/types/__tests__/usePackApp.test.d.ts.map +1 -0
- package/build/types/hooks/usePackApp.d.ts +27 -0
- package/build/types/hooks/usePackApp.d.ts.map +1 -0
- package/build/types/index.d.ts +2 -0
- package/build/types/index.d.ts.map +1 -0
- package/package.json +71 -0
- package/src/__tests__/usePackApp.test.tsx +95 -0
- package/src/hooks/usePackApp.ts +109 -0
- package/src/index.ts +18 -0
- package/tsconfig.json +21 -0
- package/vitest.config.mjs +28 -0
- package/vitest.setup.ts +2 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2025 Palantir Technologies, Inc.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @palantir/pack.app.react
|
|
2
|
+
|
|
3
|
+
React context utilities for PACK applications.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Create a typed provider and hook next to the configured app. This preserves accessors added by
|
|
8
|
+
`.withState()` for example, instead of narrowing the app back to the base `PackApp` type.
|
|
9
|
+
|
|
10
|
+
```typescript
|
|
11
|
+
import { initPackApp } from "@palantir/pack.app";
|
|
12
|
+
import { createPackAppContext } from "@palantir/pack.app.react";
|
|
13
|
+
|
|
14
|
+
export const app = initPackApp(client, options).withState().build();
|
|
15
|
+
export const { PackAppProvider, usePackApp } = createPackAppContext(app);
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
If the app is created asynchronously, create the typed context first and pass the finished app to
|
|
19
|
+
the provider:
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
async function initializePackApp() {
|
|
23
|
+
// await any required setup
|
|
24
|
+
return initPackApp(client, options).withState().build();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
type App = Awaited<ReturnType<typeof initializePackApp>>;
|
|
28
|
+
|
|
29
|
+
export const { PackAppProvider, usePackApp } = createPackAppContext<App>();
|
|
30
|
+
|
|
31
|
+
const app = await initializePackApp();
|
|
32
|
+
|
|
33
|
+
<PackAppProvider value={app}>
|
|
34
|
+
<App />
|
|
35
|
+
</PackAppProvider>;
|
|
36
|
+
```
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createContext, useContext, createElement } from 'react';
|
|
2
|
+
|
|
3
|
+
// src/hooks/usePackApp.ts
|
|
4
|
+
function createPackAppContext(app) {
|
|
5
|
+
const packContext = createContext(null);
|
|
6
|
+
const useTypedPackApp = createUsePackApp(packContext);
|
|
7
|
+
if (app == null) {
|
|
8
|
+
let PackAppValueProvider2 = function({
|
|
9
|
+
children,
|
|
10
|
+
value
|
|
11
|
+
}) {
|
|
12
|
+
return createElement(packContext.Provider, {
|
|
13
|
+
value
|
|
14
|
+
}, children);
|
|
15
|
+
};
|
|
16
|
+
return {
|
|
17
|
+
PackAppProvider: PackAppValueProvider2,
|
|
18
|
+
usePackApp: useTypedPackApp
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const boundApp = app;
|
|
22
|
+
function BoundPackAppProvider({
|
|
23
|
+
children
|
|
24
|
+
}) {
|
|
25
|
+
return createElement(packContext.Provider, {
|
|
26
|
+
value: boundApp
|
|
27
|
+
}, children);
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
PackAppProvider: BoundPackAppProvider,
|
|
31
|
+
usePackApp: useTypedPackApp
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
var PACK_CONTEXT = createContext(null);
|
|
35
|
+
function usePackApp(throwOnMissing = true) {
|
|
36
|
+
const packApp = useContext(PACK_CONTEXT);
|
|
37
|
+
if (packApp == null && throwOnMissing) {
|
|
38
|
+
throw new Error("usePackApp must be used within a PackApp provider");
|
|
39
|
+
}
|
|
40
|
+
return packApp;
|
|
41
|
+
}
|
|
42
|
+
var PackAppProvider = PACK_CONTEXT.Provider;
|
|
43
|
+
function createUsePackApp(packContext) {
|
|
44
|
+
function useTypedPackApp(throwOnMissing = true) {
|
|
45
|
+
const packApp = useContext(packContext);
|
|
46
|
+
if (packApp == null && throwOnMissing) {
|
|
47
|
+
throw new Error("usePackApp must be used within a PackApp provider");
|
|
48
|
+
}
|
|
49
|
+
return packApp;
|
|
50
|
+
}
|
|
51
|
+
return useTypedPackApp;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { PackAppProvider, createPackAppContext, usePackApp };
|
|
55
|
+
//# sourceMappingURL=index.js.map
|
|
56
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/usePackApp.ts"],"names":["PackAppValueProvider"],"mappings":";;;AAuBO,SAAS,qBAAqB,GAAA,EAAK;AACxC,EAAA,MAAM,WAAA,GAAc,cAAc,IAAI,CAAA;AACtC,EAAA,MAAM,eAAA,GAAkB,iBAAiB,WAAW,CAAA;AACpD,EAAA,IAAI,OAAO,IAAA,EAAM;AACf,IAAA,IAASA,wBAAT,SAA8B;AAAA,MAC5B,QAAA;AAAA,MACA;AAAA,KACF,EAAG;AACD,MAAA,OAAO,aAAA,CAAc,YAAY,QAAA,EAAU;AAAA,QACzC;AAAA,SACC,QAAQ,CAAA;AAAA,IACb,CAAA;AACA,IAAA,OAAO;AAAA,MACL,eAAA,EAAiBA,qBAAAA;AAAA,MACjB,UAAA,EAAY;AAAA,KACd;AAAA,EACF;AACA,EAAA,MAAM,QAAA,GAAW,GAAA;AACjB,EAAA,SAAS,oBAAA,CAAqB;AAAA,IAC5B;AAAA,GACF,EAAG;AACD,IAAA,OAAO,aAAA,CAAc,YAAY,QAAA,EAAU;AAAA,MACzC,KAAA,EAAO;AAAA,OACN,QAAQ,CAAA;AAAA,EACb;AACA,EAAA,OAAO;AAAA,IACL,eAAA,EAAiB,oBAAA;AAAA,IACjB,UAAA,EAAY;AAAA,GACd;AACF;AACA,IAAM,YAAA,GAAe,cAAc,IAAI,CAAA;AAChC,SAAS,UAAA,CAAW,iBAAiB,IAAA,EAAM;AAChD,EAAA,MAAM,OAAA,GAAU,WAAW,YAAY,CAAA;AACvC,EAAA,IAAI,OAAA,IAAW,QAAQ,cAAA,EAAgB;AACrC,IAAA,MAAM,IAAI,MAAM,mDAAmD,CAAA;AAAA,EACrE;AACA,EAAA,OAAO,OAAA;AACT;AACO,IAAM,kBAAkB,YAAA,CAAa;AAC5C,SAAS,iBAAiB,WAAA,EAAa;AACrC,EAAA,SAAS,eAAA,CAAgB,iBAAiB,IAAA,EAAM;AAC9C,IAAA,MAAM,OAAA,GAAU,WAAW,WAAW,CAAA;AACtC,IAAA,IAAI,OAAA,IAAW,QAAQ,cAAA,EAAgB;AACrC,MAAA,MAAM,IAAI,MAAM,mDAAmD,CAAA;AAAA,IACrE;AACA,IAAA,OAAO,OAAA;AAAA,EACT;AACA,EAAA,OAAO,eAAA;AACT","file":"index.js","sourcesContent":["/*\n * Copyright 2025 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { createContext, createElement, useContext } from \"react\";\n\n/**\n * Creates a provider and hook that preserve all configured module accessor types.\n * Pass the app to bind it immediately, or specify its type when the app is created later.\n */\n\nexport function createPackAppContext(app) {\n const packContext = createContext(null);\n const useTypedPackApp = createUsePackApp(packContext);\n if (app == null) {\n function PackAppValueProvider({\n children,\n value\n }) {\n return createElement(packContext.Provider, {\n value\n }, children);\n }\n return {\n PackAppProvider: PackAppValueProvider,\n usePackApp: useTypedPackApp\n };\n }\n const boundApp = app;\n function BoundPackAppProvider({\n children\n }) {\n return createElement(packContext.Provider, {\n value: boundApp\n }, children);\n }\n return {\n PackAppProvider: BoundPackAppProvider,\n usePackApp: useTypedPackApp\n };\n}\nconst PACK_CONTEXT = createContext(null);\nexport function usePackApp(throwOnMissing = true) {\n const packApp = useContext(PACK_CONTEXT);\n if (packApp == null && throwOnMissing) {\n throw new Error(\"usePackApp must be used within a PackApp provider\");\n }\n return packApp;\n}\nexport const PackAppProvider = PACK_CONTEXT.Provider;\nfunction createUsePackApp(packContext) {\n function useTypedPackApp(throwOnMissing = true) {\n const packApp = useContext(packContext);\n if (packApp == null && throwOnMissing) {\n throw new Error(\"usePackApp must be used within a PackApp provider\");\n }\n return packApp;\n }\n return useTypedPackApp;\n}"]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
|
|
5
|
+
// src/hooks/usePackApp.ts
|
|
6
|
+
function createPackAppContext(app) {
|
|
7
|
+
const packContext = react.createContext(null);
|
|
8
|
+
const useTypedPackApp = createUsePackApp(packContext);
|
|
9
|
+
if (app == null) {
|
|
10
|
+
let PackAppValueProvider2 = function({
|
|
11
|
+
children,
|
|
12
|
+
value
|
|
13
|
+
}) {
|
|
14
|
+
return react.createElement(packContext.Provider, {
|
|
15
|
+
value
|
|
16
|
+
}, children);
|
|
17
|
+
};
|
|
18
|
+
return {
|
|
19
|
+
PackAppProvider: PackAppValueProvider2,
|
|
20
|
+
usePackApp: useTypedPackApp
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const boundApp = app;
|
|
24
|
+
function BoundPackAppProvider({
|
|
25
|
+
children
|
|
26
|
+
}) {
|
|
27
|
+
return react.createElement(packContext.Provider, {
|
|
28
|
+
value: boundApp
|
|
29
|
+
}, children);
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
PackAppProvider: BoundPackAppProvider,
|
|
33
|
+
usePackApp: useTypedPackApp
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
var PACK_CONTEXT = react.createContext(null);
|
|
37
|
+
function usePackApp(throwOnMissing = true) {
|
|
38
|
+
const packApp = react.useContext(PACK_CONTEXT);
|
|
39
|
+
if (packApp == null && throwOnMissing) {
|
|
40
|
+
throw new Error("usePackApp must be used within a PackApp provider");
|
|
41
|
+
}
|
|
42
|
+
return packApp;
|
|
43
|
+
}
|
|
44
|
+
var PackAppProvider = PACK_CONTEXT.Provider;
|
|
45
|
+
function createUsePackApp(packContext) {
|
|
46
|
+
function useTypedPackApp(throwOnMissing = true) {
|
|
47
|
+
const packApp = react.useContext(packContext);
|
|
48
|
+
if (packApp == null && throwOnMissing) {
|
|
49
|
+
throw new Error("usePackApp must be used within a PackApp provider");
|
|
50
|
+
}
|
|
51
|
+
return packApp;
|
|
52
|
+
}
|
|
53
|
+
return useTypedPackApp;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
exports.PackAppProvider = PackAppProvider;
|
|
57
|
+
exports.createPackAppContext = createPackAppContext;
|
|
58
|
+
exports.usePackApp = usePackApp;
|
|
59
|
+
//# sourceMappingURL=index.cjs.map
|
|
60
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/usePackApp.ts"],"names":["createContext","PackAppValueProvider","createElement","useContext"],"mappings":";;;;;AAuBO,SAAS,qBAAqB,GAAA,EAAK;AACxC,EAAA,MAAM,WAAA,GAAcA,oBAAc,IAAI,CAAA;AACtC,EAAA,MAAM,eAAA,GAAkB,iBAAiB,WAAW,CAAA;AACpD,EAAA,IAAI,OAAO,IAAA,EAAM;AACf,IAAA,IAASC,wBAAT,SAA8B;AAAA,MAC5B,QAAA;AAAA,MACA;AAAA,KACF,EAAG;AACD,MAAA,OAAOC,mBAAA,CAAc,YAAY,QAAA,EAAU;AAAA,QACzC;AAAA,SACC,QAAQ,CAAA;AAAA,IACb,CAAA;AACA,IAAA,OAAO;AAAA,MACL,eAAA,EAAiBD,qBAAAA;AAAA,MACjB,UAAA,EAAY;AAAA,KACd;AAAA,EACF;AACA,EAAA,MAAM,QAAA,GAAW,GAAA;AACjB,EAAA,SAAS,oBAAA,CAAqB;AAAA,IAC5B;AAAA,GACF,EAAG;AACD,IAAA,OAAOC,mBAAA,CAAc,YAAY,QAAA,EAAU;AAAA,MACzC,KAAA,EAAO;AAAA,OACN,QAAQ,CAAA;AAAA,EACb;AACA,EAAA,OAAO;AAAA,IACL,eAAA,EAAiB,oBAAA;AAAA,IACjB,UAAA,EAAY;AAAA,GACd;AACF;AACA,IAAM,YAAA,GAAeF,oBAAc,IAAI,CAAA;AAChC,SAAS,UAAA,CAAW,iBAAiB,IAAA,EAAM;AAChD,EAAA,MAAM,OAAA,GAAUG,iBAAW,YAAY,CAAA;AACvC,EAAA,IAAI,OAAA,IAAW,QAAQ,cAAA,EAAgB;AACrC,IAAA,MAAM,IAAI,MAAM,mDAAmD,CAAA;AAAA,EACrE;AACA,EAAA,OAAO,OAAA;AACT;AACO,IAAM,kBAAkB,YAAA,CAAa;AAC5C,SAAS,iBAAiB,WAAA,EAAa;AACrC,EAAA,SAAS,eAAA,CAAgB,iBAAiB,IAAA,EAAM;AAC9C,IAAA,MAAM,OAAA,GAAUA,iBAAW,WAAW,CAAA;AACtC,IAAA,IAAI,OAAA,IAAW,QAAQ,cAAA,EAAgB;AACrC,MAAA,MAAM,IAAI,MAAM,mDAAmD,CAAA;AAAA,IACrE;AACA,IAAA,OAAO,OAAA;AAAA,EACT;AACA,EAAA,OAAO,eAAA;AACT","file":"index.cjs","sourcesContent":["/*\n * Copyright 2025 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { createContext, createElement, useContext } from \"react\";\n\n/**\n * Creates a provider and hook that preserve all configured module accessor types.\n * Pass the app to bind it immediately, or specify its type when the app is created later.\n */\n\nexport function createPackAppContext(app) {\n const packContext = createContext(null);\n const useTypedPackApp = createUsePackApp(packContext);\n if (app == null) {\n function PackAppValueProvider({\n children,\n value\n }) {\n return createElement(packContext.Provider, {\n value\n }, children);\n }\n return {\n PackAppProvider: PackAppValueProvider,\n usePackApp: useTypedPackApp\n };\n }\n const boundApp = app;\n function BoundPackAppProvider({\n children\n }) {\n return createElement(packContext.Provider, {\n value: boundApp\n }, children);\n }\n return {\n PackAppProvider: BoundPackAppProvider,\n usePackApp: useTypedPackApp\n };\n}\nconst PACK_CONTEXT = createContext(null);\nexport function usePackApp(throwOnMissing = true) {\n const packApp = useContext(PACK_CONTEXT);\n if (packApp == null && throwOnMissing) {\n throw new Error(\"usePackApp must be used within a PackApp provider\");\n }\n return packApp;\n}\nexport const PackAppProvider = PACK_CONTEXT.Provider;\nfunction createUsePackApp(packContext) {\n function useTypedPackApp(throwOnMissing = true) {\n const packApp = useContext(packContext);\n if (packApp == null && throwOnMissing) {\n throw new Error(\"usePackApp must be used within a PackApp provider\");\n }\n return packApp;\n }\n return useTypedPackApp;\n}"]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { PackApp } from '@palantir/pack.core';
|
|
2
|
+
import { PropsWithChildren, ReactElement } from 'react';
|
|
3
|
+
|
|
4
|
+
interface UsePackApp<TApp extends PackApp> {
|
|
5
|
+
(): TApp;
|
|
6
|
+
(throwOnMissing: true): TApp;
|
|
7
|
+
(throwOnMissing: false): TApp | null;
|
|
8
|
+
}
|
|
9
|
+
interface BoundPackAppContext<TApp extends PackApp> {
|
|
10
|
+
readonly PackAppProvider: (props: PropsWithChildren) => ReactElement;
|
|
11
|
+
readonly usePackApp: UsePackApp<TApp>;
|
|
12
|
+
}
|
|
13
|
+
interface PackAppContext<TApp extends PackApp> {
|
|
14
|
+
readonly PackAppProvider: (props: PropsWithChildren<{
|
|
15
|
+
readonly value: TApp;
|
|
16
|
+
}>) => ReactElement;
|
|
17
|
+
readonly usePackApp: UsePackApp<TApp>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Creates a provider and hook that preserve all configured module accessor types.
|
|
21
|
+
* Pass the app to bind it immediately, or specify its type when the app is created later.
|
|
22
|
+
*/
|
|
23
|
+
declare function createPackAppContext<TApp extends PackApp>(): PackAppContext<TApp>;
|
|
24
|
+
declare function createPackAppContext<TApp extends PackApp>(app: TApp): BoundPackAppContext<TApp>;
|
|
25
|
+
declare function usePackApp(): PackApp;
|
|
26
|
+
declare function usePackApp(throwOnMissing: true): PackApp;
|
|
27
|
+
declare function usePackApp(throwOnMissing: false): PackApp | null;
|
|
28
|
+
declare const PackAppProvider: React.Provider<PackApp | null>;
|
|
29
|
+
|
|
30
|
+
export { type BoundPackAppContext, type PackAppContext, PackAppProvider, type UsePackApp, createPackAppContext, usePackApp };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createContext, useContext, createElement } from 'react';
|
|
2
|
+
|
|
3
|
+
// src/hooks/usePackApp.ts
|
|
4
|
+
function createPackAppContext(app) {
|
|
5
|
+
const packContext = createContext(null);
|
|
6
|
+
const useTypedPackApp = createUsePackApp(packContext);
|
|
7
|
+
if (app == null) {
|
|
8
|
+
let PackAppValueProvider2 = function({
|
|
9
|
+
children,
|
|
10
|
+
value
|
|
11
|
+
}) {
|
|
12
|
+
return createElement(packContext.Provider, {
|
|
13
|
+
value
|
|
14
|
+
}, children);
|
|
15
|
+
};
|
|
16
|
+
return {
|
|
17
|
+
PackAppProvider: PackAppValueProvider2,
|
|
18
|
+
usePackApp: useTypedPackApp
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const boundApp = app;
|
|
22
|
+
function BoundPackAppProvider({
|
|
23
|
+
children
|
|
24
|
+
}) {
|
|
25
|
+
return createElement(packContext.Provider, {
|
|
26
|
+
value: boundApp
|
|
27
|
+
}, children);
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
PackAppProvider: BoundPackAppProvider,
|
|
31
|
+
usePackApp: useTypedPackApp
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
var PACK_CONTEXT = createContext(null);
|
|
35
|
+
function usePackApp(throwOnMissing = true) {
|
|
36
|
+
const packApp = useContext(PACK_CONTEXT);
|
|
37
|
+
if (packApp == null && throwOnMissing) {
|
|
38
|
+
throw new Error("usePackApp must be used within a PackApp provider");
|
|
39
|
+
}
|
|
40
|
+
return packApp;
|
|
41
|
+
}
|
|
42
|
+
var PackAppProvider = PACK_CONTEXT.Provider;
|
|
43
|
+
function createUsePackApp(packContext) {
|
|
44
|
+
function useTypedPackApp(throwOnMissing = true) {
|
|
45
|
+
const packApp = useContext(packContext);
|
|
46
|
+
if (packApp == null && throwOnMissing) {
|
|
47
|
+
throw new Error("usePackApp must be used within a PackApp provider");
|
|
48
|
+
}
|
|
49
|
+
return packApp;
|
|
50
|
+
}
|
|
51
|
+
return useTypedPackApp;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { PackAppProvider, createPackAppContext, usePackApp };
|
|
55
|
+
//# sourceMappingURL=index.js.map
|
|
56
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/usePackApp.ts"],"names":["PackAppValueProvider"],"mappings":";;;AAuBO,SAAS,qBAAqB,GAAA,EAAK;AACxC,EAAA,MAAM,WAAA,GAAc,cAAc,IAAI,CAAA;AACtC,EAAA,MAAM,eAAA,GAAkB,iBAAiB,WAAW,CAAA;AACpD,EAAA,IAAI,OAAO,IAAA,EAAM;AACf,IAAA,IAASA,wBAAT,SAA8B;AAAA,MAC5B,QAAA;AAAA,MACA;AAAA,KACF,EAAG;AACD,MAAA,OAAO,aAAA,CAAc,YAAY,QAAA,EAAU;AAAA,QACzC;AAAA,SACC,QAAQ,CAAA;AAAA,IACb,CAAA;AACA,IAAA,OAAO;AAAA,MACL,eAAA,EAAiBA,qBAAAA;AAAA,MACjB,UAAA,EAAY;AAAA,KACd;AAAA,EACF;AACA,EAAA,MAAM,QAAA,GAAW,GAAA;AACjB,EAAA,SAAS,oBAAA,CAAqB;AAAA,IAC5B;AAAA,GACF,EAAG;AACD,IAAA,OAAO,aAAA,CAAc,YAAY,QAAA,EAAU;AAAA,MACzC,KAAA,EAAO;AAAA,OACN,QAAQ,CAAA;AAAA,EACb;AACA,EAAA,OAAO;AAAA,IACL,eAAA,EAAiB,oBAAA;AAAA,IACjB,UAAA,EAAY;AAAA,GACd;AACF;AACA,IAAM,YAAA,GAAe,cAAc,IAAI,CAAA;AAChC,SAAS,UAAA,CAAW,iBAAiB,IAAA,EAAM;AAChD,EAAA,MAAM,OAAA,GAAU,WAAW,YAAY,CAAA;AACvC,EAAA,IAAI,OAAA,IAAW,QAAQ,cAAA,EAAgB;AACrC,IAAA,MAAM,IAAI,MAAM,mDAAmD,CAAA;AAAA,EACrE;AACA,EAAA,OAAO,OAAA;AACT;AACO,IAAM,kBAAkB,YAAA,CAAa;AAC5C,SAAS,iBAAiB,WAAA,EAAa;AACrC,EAAA,SAAS,eAAA,CAAgB,iBAAiB,IAAA,EAAM;AAC9C,IAAA,MAAM,OAAA,GAAU,WAAW,WAAW,CAAA;AACtC,IAAA,IAAI,OAAA,IAAW,QAAQ,cAAA,EAAgB;AACrC,MAAA,MAAM,IAAI,MAAM,mDAAmD,CAAA;AAAA,IACrE;AACA,IAAA,OAAO,OAAA;AAAA,EACT;AACA,EAAA,OAAO,eAAA;AACT","file":"index.js","sourcesContent":["/*\n * Copyright 2025 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { createContext, createElement, useContext } from \"react\";\n\n/**\n * Creates a provider and hook that preserve all configured module accessor types.\n * Pass the app to bind it immediately, or specify its type when the app is created later.\n */\n\nexport function createPackAppContext(app) {\n const packContext = createContext(null);\n const useTypedPackApp = createUsePackApp(packContext);\n if (app == null) {\n function PackAppValueProvider({\n children,\n value\n }) {\n return createElement(packContext.Provider, {\n value\n }, children);\n }\n return {\n PackAppProvider: PackAppValueProvider,\n usePackApp: useTypedPackApp\n };\n }\n const boundApp = app;\n function BoundPackAppProvider({\n children\n }) {\n return createElement(packContext.Provider, {\n value: boundApp\n }, children);\n }\n return {\n PackAppProvider: BoundPackAppProvider,\n usePackApp: useTypedPackApp\n };\n}\nconst PACK_CONTEXT = createContext(null);\nexport function usePackApp(throwOnMissing = true) {\n const packApp = useContext(PACK_CONTEXT);\n if (packApp == null && throwOnMissing) {\n throw new Error(\"usePackApp must be used within a PackApp provider\");\n }\n return packApp;\n}\nexport const PackAppProvider = PACK_CONTEXT.Provider;\nfunction createUsePackApp(packContext) {\n function useTypedPackApp(throwOnMissing = true) {\n const packApp = useContext(packContext);\n if (packApp == null && throwOnMissing) {\n throw new Error(\"usePackApp must be used within a PackApp provider\");\n }\n return packApp;\n }\n return useTypedPackApp;\n}"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"","names":[],"sources":["../../../src/__tests__/usePackApp.test.tsx"],"version":3,"file":"usePackApp.test.d.ts"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { PackApp } from "@palantir/pack.core";
|
|
2
|
+
import type { PropsWithChildren, ReactElement } from "react";
|
|
3
|
+
export interface UsePackApp<TApp extends PackApp> {
|
|
4
|
+
(): TApp;
|
|
5
|
+
(throwOnMissing: true): TApp;
|
|
6
|
+
(throwOnMissing: false): TApp | null;
|
|
7
|
+
}
|
|
8
|
+
export interface BoundPackAppContext<TApp extends PackApp> {
|
|
9
|
+
readonly PackAppProvider: (props: PropsWithChildren) => ReactElement;
|
|
10
|
+
readonly usePackApp: UsePackApp<TApp>;
|
|
11
|
+
}
|
|
12
|
+
export interface PackAppContext<TApp extends PackApp> {
|
|
13
|
+
readonly PackAppProvider: (props: PropsWithChildren<{
|
|
14
|
+
readonly value: TApp;
|
|
15
|
+
}>) => ReactElement;
|
|
16
|
+
readonly usePackApp: UsePackApp<TApp>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates a provider and hook that preserve all configured module accessor types.
|
|
20
|
+
* Pass the app to bind it immediately, or specify its type when the app is created later.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createPackAppContext<TApp extends PackApp>(): PackAppContext<TApp>;
|
|
23
|
+
export declare function createPackAppContext<TApp extends PackApp>(app: TApp): BoundPackAppContext<TApp>;
|
|
24
|
+
export declare function usePackApp(): PackApp;
|
|
25
|
+
export declare function usePackApp(throwOnMissing: true): PackApp;
|
|
26
|
+
export declare function usePackApp(throwOnMissing: false): PackApp | null;
|
|
27
|
+
export declare const PackAppProvider: React.Provider<PackApp | null>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAgBA,cAAc,eAAe;AAC7B,cAAuB,mBAAmB,oBAAoB;AAG9D,iBAAiB,WAAW,aAAa,SAAS;KAC5C;EACH,gBAAgB,OAAO;EACvB,gBAAgB,QAAQ;;AAG3B,iBAAiB,oBAAoB,aAAa,SAAS;UAChD,kBAAkB,OAAO,sBAAsB;UAC/C,YAAY,WAAW;;AAGlC,iBAAiB,eAAe,aAAa,SAAS;UAC3C,kBACP,OAAO,kBAAkB;WAAW,OAAO;QACxC;UACI,YAAY,WAAW;;;;;;AAOlC,OAAO,iBAAS,qBAAqB,aAAa,YAAY,eAAe;AAC7E,OAAO,iBAAS,qBAAqB,aAAa,SAChD,KAAK,OACJ,oBAAoB;AAmCvB,OAAO,iBAAS,cAAc;AAC9B,OAAO,iBAAS,WAAW,gBAAgB,OAAO;AAClD,OAAO,iBAAS,WAAW,gBAAgB,QAAQ;AASnD,OAAO,cAAMA,iBAAiB,MAAM,SAAS","names":["PackAppProvider: React.Provider<PackApp | null>"],"sources":["../../../src/hooks/usePackApp.ts"],"version":3,"file":"usePackApp.d.ts"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAgBA,SAAS,sBAAsB,iBAAiB,kBAAkB;AAClE,cAAc,qBAAqB,gBAAgB,kBAAkB","names":[],"sources":["../../src/index.ts"],"version":3,"file":"index.d.ts"}
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@palantir/pack.app.react",
|
|
3
|
+
"version": "0.27.0",
|
|
4
|
+
"description": "React bindings for PACK applications",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/palantir/pack.git"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"browser": "./build/browser/index.js",
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./build/types/index.d.ts",
|
|
15
|
+
"default": "./build/esm/index.js"
|
|
16
|
+
},
|
|
17
|
+
"require": "./build/cjs/index.js",
|
|
18
|
+
"default": "./build/browser/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./*": {
|
|
21
|
+
"browser": "./build/browser/public/*.js",
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./build/types/public/*.d.ts",
|
|
24
|
+
"default": "./build/js/public/*.js"
|
|
25
|
+
},
|
|
26
|
+
"require": "./build/cjs/public/*.js",
|
|
27
|
+
"default": "./build/browser/public/*.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@palantir/pack.core": "0.27.0"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"react": "^17 || ^18 || ^19"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@testing-library/react": "^16.3.0",
|
|
38
|
+
"@types/react": "^18.3.18",
|
|
39
|
+
"@types/react-dom": "^18.3.5",
|
|
40
|
+
"happy-dom": "^20.0.11",
|
|
41
|
+
"react": "^18",
|
|
42
|
+
"react-dom": "^18",
|
|
43
|
+
"rimraf": "^6.1.2",
|
|
44
|
+
"tslib": "^2.8.1",
|
|
45
|
+
"typescript": "^5.9.3",
|
|
46
|
+
"vitest-mock-extended": "^3.1.0",
|
|
47
|
+
"@palantir/pack.monorepo.tsconfig": "~0.4.3"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"keywords": [
|
|
53
|
+
"pack"
|
|
54
|
+
],
|
|
55
|
+
"main": "./build/cjs/index.js",
|
|
56
|
+
"module": "./build/esm/index.js",
|
|
57
|
+
"types": "./build/cjs/index.d.ts",
|
|
58
|
+
"type": "module",
|
|
59
|
+
"scripts": {
|
|
60
|
+
"clean": "rimraf .turbo build dist lib test-output *.tgz tsconfig.tsbuildinfo",
|
|
61
|
+
"lint": "eslint ./src && dprint check --config $(find-up dprint.json) --allow-no-files",
|
|
62
|
+
"lint:fix": "eslint ./src --fix && dprint fmt --config $(find-up dprint.json) --allow-no-files",
|
|
63
|
+
"test": "vitest run --passWithNoTests -u",
|
|
64
|
+
"test:watch": "vitest --passWithNoTests",
|
|
65
|
+
"transpileBrowser": "monorepo-transpile -f esm -m bundle -t browser",
|
|
66
|
+
"transpileCjs": "monorepo-transpile -f cjs -m bundle -t node",
|
|
67
|
+
"transpileEsm": "monorepo-transpile -f esm -m bundle -t node",
|
|
68
|
+
"transpileTypes": "monorepo-transpile -f esm -m types -t node",
|
|
69
|
+
"typecheck": "tsc --noEmit --emitDeclarationOnly false"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 Palantir Technologies, Inc. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { PackApp } from "@palantir/pack.core";
|
|
18
|
+
import { renderHook } from "@testing-library/react";
|
|
19
|
+
import type { PropsWithChildren } from "react";
|
|
20
|
+
import { describe, expect, expectTypeOf, it } from "vitest";
|
|
21
|
+
import { mockDeep } from "vitest-mock-extended";
|
|
22
|
+
import { createPackAppContext, PackAppProvider, usePackApp } from "../index.js";
|
|
23
|
+
|
|
24
|
+
interface TestModule {
|
|
25
|
+
readonly value: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type TestPackApp = PackApp & { readonly testModule: TestModule };
|
|
29
|
+
|
|
30
|
+
describe("createPackAppContext", () => {
|
|
31
|
+
it("preserves the configured app type", () => {
|
|
32
|
+
const app: TestPackApp = mockDeep<TestPackApp>();
|
|
33
|
+
const { PackAppProvider, usePackApp } = createPackAppContext(app);
|
|
34
|
+
|
|
35
|
+
const { result } = renderHook(() => usePackApp(), { wrapper: PackAppProvider });
|
|
36
|
+
|
|
37
|
+
expect(result.current).toBe(app);
|
|
38
|
+
expectTypeOf(result.current.testModule).toEqualTypeOf<TestModule>();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("throws when its provider is missing", () => {
|
|
42
|
+
const app: TestPackApp = mockDeep<TestPackApp>();
|
|
43
|
+
const { usePackApp } = createPackAppContext(app);
|
|
44
|
+
|
|
45
|
+
expect(() => renderHook(() => usePackApp())).toThrow(
|
|
46
|
+
"usePackApp must be used within a PackApp provider",
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("returns null when a missing provider is allowed", () => {
|
|
51
|
+
const app: TestPackApp = mockDeep<TestPackApp>();
|
|
52
|
+
const { usePackApp } = createPackAppContext(app);
|
|
53
|
+
|
|
54
|
+
const { result } = renderHook(() => usePackApp(false));
|
|
55
|
+
|
|
56
|
+
expectTypeOf(result.current).toEqualTypeOf<TestPackApp | null>();
|
|
57
|
+
expect(result.current).toBeNull();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("preserves an explicit app type when the app is created later", () => {
|
|
61
|
+
const app: TestPackApp = mockDeep<TestPackApp>();
|
|
62
|
+
const { PackAppProvider, usePackApp } = createPackAppContext<TestPackApp>();
|
|
63
|
+
|
|
64
|
+
function Wrapper({ children }: PropsWithChildren) {
|
|
65
|
+
return <PackAppProvider value={app}>{children}</PackAppProvider>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const { result } = renderHook(() => usePackApp(), { wrapper: Wrapper });
|
|
69
|
+
|
|
70
|
+
expect(result.current).toBe(app);
|
|
71
|
+
expectTypeOf(result.current.testModule).toEqualTypeOf<TestModule>();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("default PackApp context", () => {
|
|
76
|
+
it("returns the required provided app", () => {
|
|
77
|
+
const app: PackApp = mockDeep<PackApp>();
|
|
78
|
+
|
|
79
|
+
function Wrapper({ children }: PropsWithChildren) {
|
|
80
|
+
return <PackAppProvider value={app}>{children}</PackAppProvider>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const { result } = renderHook(() => usePackApp(true), { wrapper: Wrapper });
|
|
84
|
+
|
|
85
|
+
expectTypeOf(result.current).toEqualTypeOf<PackApp>();
|
|
86
|
+
expect(result.current).toBe(app);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("returns null when a missing provider is allowed", () => {
|
|
90
|
+
const { result } = renderHook(() => usePackApp(false));
|
|
91
|
+
|
|
92
|
+
expectTypeOf(result.current).toEqualTypeOf<PackApp | null>();
|
|
93
|
+
expect(result.current).toBeNull();
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Palantir Technologies, Inc. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { PackApp } from "@palantir/pack.core";
|
|
18
|
+
import type { Context, PropsWithChildren, ReactElement } from "react";
|
|
19
|
+
import { createContext, createElement, useContext } from "react";
|
|
20
|
+
|
|
21
|
+
export interface UsePackApp<TApp extends PackApp> {
|
|
22
|
+
(): TApp;
|
|
23
|
+
(throwOnMissing: true): TApp;
|
|
24
|
+
(throwOnMissing: false): TApp | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface BoundPackAppContext<TApp extends PackApp> {
|
|
28
|
+
readonly PackAppProvider: (props: PropsWithChildren) => ReactElement;
|
|
29
|
+
readonly usePackApp: UsePackApp<TApp>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface PackAppContext<TApp extends PackApp> {
|
|
33
|
+
readonly PackAppProvider: (
|
|
34
|
+
props: PropsWithChildren<{ readonly value: TApp }>,
|
|
35
|
+
) => ReactElement;
|
|
36
|
+
readonly usePackApp: UsePackApp<TApp>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Creates a provider and hook that preserve all configured module accessor types.
|
|
41
|
+
* Pass the app to bind it immediately, or specify its type when the app is created later.
|
|
42
|
+
*/
|
|
43
|
+
export function createPackAppContext<TApp extends PackApp>(): PackAppContext<TApp>;
|
|
44
|
+
export function createPackAppContext<TApp extends PackApp>(
|
|
45
|
+
app: TApp,
|
|
46
|
+
): BoundPackAppContext<TApp>;
|
|
47
|
+
export function createPackAppContext<TApp extends PackApp>(
|
|
48
|
+
app?: TApp,
|
|
49
|
+
): BoundPackAppContext<TApp> | PackAppContext<TApp> {
|
|
50
|
+
const packContext = createContext<TApp | null>(null);
|
|
51
|
+
const useTypedPackApp = createUsePackApp(packContext);
|
|
52
|
+
|
|
53
|
+
if (app == null) {
|
|
54
|
+
function PackAppValueProvider({
|
|
55
|
+
children,
|
|
56
|
+
value,
|
|
57
|
+
}: PropsWithChildren<{ readonly value: TApp }>): ReactElement {
|
|
58
|
+
return createElement(packContext.Provider, { value }, children);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
PackAppProvider: PackAppValueProvider,
|
|
63
|
+
usePackApp: useTypedPackApp,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const boundApp = app;
|
|
68
|
+
|
|
69
|
+
function BoundPackAppProvider({ children }: PropsWithChildren): ReactElement {
|
|
70
|
+
return createElement(packContext.Provider, { value: boundApp }, children);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
PackAppProvider: BoundPackAppProvider,
|
|
75
|
+
usePackApp: useTypedPackApp,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const PACK_CONTEXT = createContext<PackApp | null>(null);
|
|
80
|
+
|
|
81
|
+
export function usePackApp(): PackApp;
|
|
82
|
+
export function usePackApp(throwOnMissing: true): PackApp;
|
|
83
|
+
export function usePackApp(throwOnMissing: false): PackApp | null;
|
|
84
|
+
export function usePackApp(throwOnMissing = true): PackApp | null {
|
|
85
|
+
const packApp = useContext(PACK_CONTEXT);
|
|
86
|
+
if (packApp == null && throwOnMissing) {
|
|
87
|
+
throw new Error("usePackApp must be used within a PackApp provider");
|
|
88
|
+
}
|
|
89
|
+
return packApp;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export const PackAppProvider: React.Provider<PackApp | null> = PACK_CONTEXT.Provider;
|
|
93
|
+
|
|
94
|
+
function createUsePackApp<TApp extends PackApp>(
|
|
95
|
+
packContext: Context<TApp | null>,
|
|
96
|
+
): UsePackApp<TApp> {
|
|
97
|
+
function useTypedPackApp(): TApp;
|
|
98
|
+
function useTypedPackApp(throwOnMissing: true): TApp;
|
|
99
|
+
function useTypedPackApp(throwOnMissing: false): TApp | null;
|
|
100
|
+
function useTypedPackApp(throwOnMissing = true): TApp | null {
|
|
101
|
+
const packApp = useContext(packContext);
|
|
102
|
+
if (packApp == null && throwOnMissing) {
|
|
103
|
+
throw new Error("usePackApp must be used within a PackApp provider");
|
|
104
|
+
}
|
|
105
|
+
return packApp;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return useTypedPackApp;
|
|
109
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 Palantir Technologies, Inc. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export { createPackAppContext, PackAppProvider, usePackApp } from "./hooks/usePackApp.js";
|
|
18
|
+
export type { BoundPackAppContext, PackAppContext, UsePackApp } from "./hooks/usePackApp.js";
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "@palantir/pack.monorepo.tsconfig/base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "./build/esm",
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"declarationDir": "./build/types",
|
|
7
|
+
"tsBuildInfoFile": "./build/tsconfig.tsbuildinfo"
|
|
8
|
+
},
|
|
9
|
+
"include": [
|
|
10
|
+
"src/**/*"
|
|
11
|
+
],
|
|
12
|
+
"exclude": [
|
|
13
|
+
"build",
|
|
14
|
+
"dist",
|
|
15
|
+
"node_modules",
|
|
16
|
+
"test-output",
|
|
17
|
+
"**/__tests__/**/__snapshots__",
|
|
18
|
+
"**/__tests__/**/fixtures"
|
|
19
|
+
],
|
|
20
|
+
"references": []
|
|
21
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { coverageConfigDefaults, defaultExclude, defineProject } from "vitest/config";
|
|
2
|
+
|
|
3
|
+
export default defineProject({
|
|
4
|
+
test: {
|
|
5
|
+
environment: "happy-dom",
|
|
6
|
+
exclude: [...defaultExclude, "**/build/**", "**/test-output/**"],
|
|
7
|
+
setupFiles: ["./vitest.setup.ts"],
|
|
8
|
+
coverage: {
|
|
9
|
+
provider: "v8",
|
|
10
|
+
all: true,
|
|
11
|
+
enabled: true,
|
|
12
|
+
pool: "forks",
|
|
13
|
+
include: ["src/**/*.{ts,tsx}"],
|
|
14
|
+
exclude: [
|
|
15
|
+
...coverageConfigDefaults.exclude,
|
|
16
|
+
"**/*.test.{ts,tsx}",
|
|
17
|
+
"**/__tests__",
|
|
18
|
+
"build",
|
|
19
|
+
"coverage",
|
|
20
|
+
"dist",
|
|
21
|
+
"node_modules",
|
|
22
|
+
"src/index.ts",
|
|
23
|
+
"test-output",
|
|
24
|
+
"vitest.config.*",
|
|
25
|
+
],
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
});
|
package/vitest.setup.ts
ADDED