-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add typings for tree-shakable version of library
I wasn’t sure of the best way to approach the typings of the modules (`Rest` etc). They should be opaque to the user (they shouldn’t be interacting with them directly and we want be free to change their interface in the future). There is an open issue to add support for opaque types to TypeScript [1], and people have suggested various sorts of ways of approximating them, which revolve around the use of `unique symbol` declarations. However, I don’t fully understand these solutions and so thought it best not to include them in our public API. So, for now, let’s just use `unknown`, the same way as we do for `CipherParams.key`. Resolves #1442. [1] microsoft/TypeScript#202
- Loading branch information
1 parent
be44206
commit b839715
Showing
15 changed files
with
202 additions
and
95 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,45 @@ | ||
import { Types } from './ably'; | ||
|
||
export declare const generateRandomKey: Types.Crypto['generateRandomKey']; | ||
export declare const getDefaultCryptoParams: Types.Crypto['getDefaultParams']; | ||
export declare const decodeMessage: Types.MessageStatic['fromEncoded']; | ||
export declare const decodeEncryptedMessage: Types.MessageStatic['fromEncoded']; | ||
export declare const decodeMessages: Types.MessageStatic['fromEncodedArray']; | ||
export declare const decodeEncryptedMessages: Types.MessageStatic['fromEncodedArray']; | ||
export declare const decodePresenceMessage: Types.PresenceMessageStatic['fromEncoded']; | ||
export declare const decodePresenceMessages: Types.PresenceMessageStatic['fromEncodedArray']; | ||
export declare const constructPresenceMessage: Types.PresenceMessageStatic['fromValues']; | ||
|
||
export declare const Rest: unknown; | ||
export declare const Crypto: unknown; | ||
export declare const MsgPack: unknown; | ||
export declare const RealtimePresence: unknown; | ||
export declare const WebSocketTransport: unknown; | ||
export declare const XHRPolling: unknown; | ||
export declare const XHRStreaming: unknown; | ||
export declare const XHRRequest: unknown; | ||
export declare const FetchRequest: unknown; | ||
export declare const MessageInteractions: unknown; | ||
|
||
export interface ModulesMap { | ||
Rest?: typeof Rest; | ||
Crypto?: typeof Crypto; | ||
MsgPack?: typeof MsgPack; | ||
RealtimePresence?: typeof RealtimePresence; | ||
WebSocketTransport?: typeof WebSocketTransport; | ||
XHRPolling?: typeof XHRPolling; | ||
XHRStreaming?: typeof XHRStreaming; | ||
XHRRequest?: typeof XHRRequest; | ||
FetchRequest?: typeof FetchRequest; | ||
MessageInteractions?: typeof MessageInteractions; | ||
} | ||
|
||
export declare class BaseRest extends Types.Rest { | ||
constructor(options: Types.ClientOptions, modules: ModulesMap); | ||
} | ||
|
||
export declare class BaseRealtime extends Types.Realtime { | ||
constructor(options: Types.ClientOptions, modules: ModulesMap); | ||
} | ||
|
||
export { Types }; |
This file contains 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 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 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
11 changes: 11 additions & 0 deletions
11
test/package/browser/template/server/resources/index-default.html
This file contains 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,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Ably NPM package test (default export)</title> | ||
</head> | ||
<body> | ||
<script type="text/javascript" src="index-default.js"></script> | ||
<script type="text/javascript" src="runTest.js"></script> | ||
</body> | ||
</html> |
11 changes: 11 additions & 0 deletions
11
test/package/browser/template/server/resources/index-modules.html
This file contains 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,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Ably NPM package test (tree-shakable export)</title> | ||
</head> | ||
<body> | ||
<script type="text/javascript" src="index-modules.js"></script> | ||
<script type="text/javascript" src="runTest.js"></script> | ||
</body> | ||
</html> |
This file was deleted.
Oops, something went wrong.
This file contains 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,9 @@ | ||
(async () => { | ||
try { | ||
await testAblyPackage(); | ||
onResult(null); | ||
} catch (error) { | ||
console.log('Caught error', error); | ||
onResult(error); | ||
} | ||
})(); |
This file contains 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
File renamed without changes.
This file contains 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,34 @@ | ||
import { BaseRealtime, Types, WebSocketTransport, FetchRequest, generateRandomKey } from 'ably/modules'; | ||
import { createSandboxAblyAPIKey } from './sandbox'; | ||
|
||
// This function exists to check that we can import the Types namespace and refer to its types. | ||
async function attachChannel(channel: Types.RealtimeChannel) { | ||
await channel.attach(); | ||
} | ||
|
||
// This function exists to check that one of the free-standing functions (arbitrarily chosen) can be imported and does something vaguely sensible. | ||
async function checkStandaloneFunction() { | ||
const generatedKey = await generateRandomKey(); | ||
if (!(generatedKey instanceof ArrayBuffer)) { | ||
throw new Error('Expected to get an ArrayBuffer from generateRandomKey'); | ||
} | ||
} | ||
|
||
globalThis.testAblyPackage = async function () { | ||
const key = await createSandboxAblyAPIKey(); | ||
|
||
const realtime = new BaseRealtime({ key, environment: 'sandbox' }, { WebSocketTransport, FetchRequest }); | ||
|
||
const channel = realtime.channels.get('channel'); | ||
await attachChannel(channel); | ||
|
||
const receivedMessagePromise = new Promise<void>((resolve) => { | ||
channel.subscribe(() => { | ||
resolve(); | ||
}); | ||
}); | ||
|
||
await channel.publish('message', { foo: 'bar' }); | ||
await receivedMessagePromise; | ||
await checkStandaloneFunction(); | ||
}; |
This file contains 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 |
---|---|---|
@@ -1,18 +1,25 @@ | ||
import { test, expect } from '@playwright/test'; | ||
|
||
test.describe('NPM package', () => { | ||
test('can be imported and provides access to Ably functionality', async ({ page }) => { | ||
const pageResultPromise = new Promise<void>((resolve, reject) => { | ||
page.exposeFunction('onResult', (error: Error | null) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
for (const scenario of [ | ||
{ name: 'default export', path: '/index-default.html' }, | ||
{ name: 'modular export', path: '/index-modules.html' }, | ||
]) { | ||
test.describe(scenario.name, () => { | ||
test('can be imported and provides access to Ably functionality', async ({ page }) => { | ||
const pageResultPromise = new Promise<void>((resolve, reject) => { | ||
page.exposeFunction('onResult', (error: Error | null) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
|
||
await page.goto(scenario.path); | ||
await pageResultPromise; | ||
}); | ||
}); | ||
|
||
await page.goto('/'); | ||
await pageResultPromise; | ||
}); | ||
} | ||
}); |