@farm.js/scripts 0.1.0-beta.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Farm.js Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # `@farm.js/scripts`
2
+
3
+ Typed, lifecycle-aware third-party browser scripts for Farm.js.
4
+
5
+ Use this plugin when a browser SDK arrives through an external `<script>` and exposes a global.
6
+ Farm owns when that script loads, deduplicates concurrent requests, waits for readiness, and gives
7
+ application code a typed handle instead of an ambient `window.vendor` access.
8
+
9
+ ```bash
10
+ pnpm add @farm.js/scripts
11
+ ```
12
+
13
+ ```ts
14
+ // src/lib/scripts.ts
15
+ import { defineScript } from "@farm.js/scripts/client";
16
+
17
+ interface SupportChatSDK {
18
+ load(options: { userId: string }): void;
19
+ open(): void;
20
+ }
21
+
22
+ export const supportChat = defineScript<SupportChatSDK>({
23
+ name: "support-chat",
24
+ src: "https://cdn.example.com/chat.js",
25
+ global: "SupportChat",
26
+ load: "manual",
27
+ preconnect: true,
28
+ });
29
+ ```
30
+
31
+ ```ts
32
+ // farm.config.ts
33
+ import { defineConfig } from "@farm.js/core";
34
+ import { scripts } from "@farm.js/scripts";
35
+ import { supportChat } from "./src/lib/scripts";
36
+
37
+ export default defineConfig({
38
+ plugins: [scripts({ scripts: [supportChat] })],
39
+ });
40
+ ```
41
+
42
+ ```ts
43
+ await supportChat.use((sdk) => {
44
+ sdk.load({ userId: "user_123" });
45
+ sdk.open();
46
+ });
47
+ ```
48
+
49
+ Built-in load strategies are `immediate`, `after-hydration`, `idle`, `interaction`, `manual`, and
50
+ element visibility. Definitions can also declare consent categories, dependency order, retries,
51
+ timeouts, preconnect hints, module scripts, SRI, CORS, referrer policy, fetch priority, and vendor
52
+ `data-*` attributes.
53
+
54
+ See the [Farm Scripts documentation](https://farmjs.dev/docs/plugins/scripts) for all options,
55
+ recipes, consent behavior, and limits.
@@ -0,0 +1,43 @@
1
+ import type { ResolvedScriptDefinition, ScriptConsentState, ScriptDefinition, ScriptHandle, ScriptRuntime } from "./types.js";
2
+ export type { AnyScriptHandle, ResolvedScriptDefinition, ScriptConsentState, ScriptDefinition, ScriptDuration, ScriptHandle, ScriptLoadStrategy, ScriptSnapshot, ScriptStatus, ScriptStatusListener, } from "./types.js";
3
+ export declare class ScriptEnvironmentError extends Error {
4
+ name: string;
5
+ }
6
+ export declare class ScriptNotRegisteredError extends Error {
7
+ name: string;
8
+ }
9
+ export declare class ScriptConsentRequiredError extends Error {
10
+ readonly scriptName: string;
11
+ readonly category: string;
12
+ name: string;
13
+ constructor(scriptName: string, category: string);
14
+ }
15
+ export declare class ScriptLoadError extends Error {
16
+ readonly scriptName: string;
17
+ readonly src: string;
18
+ name: string;
19
+ constructor(scriptName: string, src: string);
20
+ }
21
+ export declare class ScriptTimeoutError extends Error {
22
+ readonly scriptName: string;
23
+ readonly timeoutMs: number;
24
+ name: string;
25
+ constructor(scriptName: string, timeoutMs: number);
26
+ }
27
+ export declare class ScriptGlobalMissingError extends Error {
28
+ readonly scriptName: string;
29
+ readonly globalPath: string;
30
+ name: string;
31
+ constructor(scriptName: string, globalPath: string);
32
+ }
33
+ /** Define one typed external browser script. Register the returned handle with `scripts()`. */
34
+ export declare function defineScript<T = void>(definition: ScriptDefinition): ScriptHandle<T>;
35
+ /** Set an application-owned consent category. Consent is intentionally not persisted by Farm. */
36
+ export declare function setScriptConsent(category: string, state: ScriptConsentState): void;
37
+ export declare function grantScriptConsent(category: string): void;
38
+ export declare function denyScriptConsent(category: string): void;
39
+ export declare function clearScriptConsent(category: string): void;
40
+ export declare function getScriptConsent(category: string): ScriptConsentState;
41
+ /** @internal Start the browser lifecycle for definitions serialized by the Farm plugin. */
42
+ export declare function startScriptRuntime(definitions: readonly ResolvedScriptDefinition[], runtimeWindow?: Window & typeof globalThis, basePath?: string): ScriptRuntime;
43
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EACV,wBAAwB,EACxB,kBAAkB,EAClB,gBAAgB,EAChB,YAAY,EACZ,aAAa,EAId,MAAM,YAAY,CAAC;AAEpB,YAAY,EACV,eAAe,EACf,wBAAwB,EACxB,kBAAkB,EAClB,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,kBAAkB,EAClB,cAAc,EACd,YAAY,EACZ,oBAAoB,GACrB,MAAM,YAAY,CAAC;AAyBpB,qBAAa,sBAAuB,SAAQ,KAAK;IACtC,IAAI,SAA4B;CAC1C;AAED,qBAAa,wBAAyB,SAAQ,KAAK;IACxC,IAAI,SAA8B;CAC5C;AAED,qBAAa,0BAA2B,SAAQ,KAAK;IAGjD,QAAQ,CAAC,UAAU,EAAE,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