|
1 | 1 | /** |
2 | | - * While developing this lib some functionalities should be |
3 | | - * added to pryv js-lib in a second step |
| 2 | + * CJS→ESM interop for the pryv package with plugin patching. |
4 | 3 | * |
5 | | - * CJS→ESM interop: default import gives the module.exports object directly |
6 | | - * (Connection, Auth, etc.) instead of a frozen namespace wrapper. |
7 | | - * Type errors from consumers using `pryv.Connection` as a namespace type |
8 | | - * are suppressed via noEmitOnError: false — the JS output is correct. |
| 4 | + * Default import gives the module.exports object directly (Connection, Auth, etc.). |
| 5 | + * We re-export both the runtime value AND a merged type namespace so that consumers |
| 6 | + * can use `pryv.Connection` as both a constructor and a type annotation. |
9 | 7 | */ |
10 | | -// @ts-ignore namespace usage in consumers requires `import *`, but runtime needs default |
11 | | -import pryv from 'pryv'; |
| 8 | +import _pryv from 'pryv'; |
| 9 | +import type * as _PryvTypes from 'pryv'; |
12 | 10 | import monitor from '@pryv/monitor'; |
13 | 11 | import socketIo from '@pryv/socket.io'; |
14 | 12 | // @ts-expect-error CJS plugin pattern: module.exports = function(pryv) { ... } |
15 | | -monitor(pryv); |
| 13 | +monitor(_pryv); |
16 | 14 | // @ts-expect-error CJS plugin pattern: module.exports = function(pryv) { ... } |
17 | | -socketIo(pryv); |
| 15 | +socketIo(_pryv); |
18 | 16 |
|
19 | | -export { pryv }; |
| 17 | +// Declaration merging: the exported `pryv` const provides the runtime value, |
| 18 | +// and the namespace augments it with all the types from pryv's .d.ts. |
| 19 | +// This lets consumers write both `new pryv.Connection(...)` and `x: pryv.Connection`. |
| 20 | + |
| 21 | +// eslint-disable-next-line import-x/export -- declaration merging: value + type namespace |
| 22 | +export const pryv = _pryv as unknown as typeof _PryvTypes; |
| 23 | + |
| 24 | +// eslint-disable-next-line @typescript-eslint/no-namespace, import-x/export -- declaration merging |
| 25 | +export namespace pryv { |
| 26 | + export type Connection = _PryvTypes.Connection; |
| 27 | + export type Service = _PryvTypes.Service; |
| 28 | + export type Stream = _PryvTypes.Stream; |
| 29 | + export type Event = _PryvTypes.Event; |
| 30 | + export type APICall = _PryvTypes.APICall; |
| 31 | + export type AccessInfo = _PryvTypes.AccessInfo; |
| 32 | + export type Access = _PryvTypes.Access; |
| 33 | + export type Permission = _PryvTypes.Permission; |
| 34 | + export type ServiceInfo = _PryvTypes.ServiceInfo; |
| 35 | + export type ServiceAssets = _PryvTypes.ServiceAssets; |
| 36 | + export type AuthController = _PryvTypes.AuthController; |
| 37 | + export type AuthSettings = _PryvTypes.AuthSettings; |
| 38 | + export type ItemDeletion = _PryvTypes.ItemDeletion; |
| 39 | + export type PryvError = _PryvTypes.PryvError; |
| 40 | + export type KeyValue = _PryvTypes.KeyValue; |
| 41 | +} |
0 commit comments