@slopit/adapter-vanilla 0.1.0 → 0.1.1

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/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/recorder.ts"],"names":[],"mappings":";;;AAqCO,IAAM,cAAA,GAAN,cAA6B,YAAA,CAAa;AAAA,EACvC,YAAA;AAAA,EACA,eAAA;AAAA,EACA,gBAAA,GAAwC,IAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOhD,YAAY,MAAA,EAAgC;AAC1C,IAAA,KAAA,CAAM,MAAM,CAAA;AACZ,IAAA,IAAA,CAAK,YAAA,GAAe,QAAQ,YAAA,IAAgB,SAAA;AAC5C,IAAA,IAAA,CAAK,eAAA,GAAkB,QAAQ,eAAA,IAAmB,OAAA;AAAA,EACpD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOmB,eAAA,GAAgC;AACjD,IAAA,OAAO;AAAA,MACL,MAAM,IAAA,CAAK,YAAA;AAAA,MACX,SAAS,IAAA,CAAK,eAAA;AAAA,MACd,cAAA,EAAgB;AAAA,KAClB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQQ,qBAAqB,MAAA,EAAyC;AAEpE,IAAA,MAAM,QAAA,GACJ,OAAO,MAAA,CAAO,QAAA,KAAa,QAAA,GACvB,EAAE,IAAA,EAAM,MAAA,EAAQ,OAAA,EAAS,MAAA,CAAO,QAAA,EAAS,GACzC,MAAA,CAAO,QAAA;AAGb,IAAA,MAAM,cAA2B,EAAC;AAElC,IAAA,IAAI,MAAA,CAAO,YAAY,MAAA,EAAW;AAChC,MAAA,WAAA,CAAY,UAAU,MAAA,CAAO,OAAA;AAAA,IAC/B;AACA,IAAA,IAAI,MAAA,CAAO,eAAe,MAAA,EAAW;AACnC,MAAA,WAAA,CAAY,aAAa,MAAA,CAAO,UAAA;AAAA,IAClC;AACA,IAAA,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,CAAK,WAAU,CAAE,MAAA;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmBA,YAAA,GAAqB;AAEnB,IAAA,IAAI,IAAA,CAAK,qBAAqB,IAAA,EAAM;AAClC,MAAA,IAAA,CAAK,gBAAA,EAAiB;AACtB,MAAA,IAAA,CAAK,gBAAA,GAAmB,IAAA;AAAA,IAC1B;AACA,IAAA,IAAA,CAAK,KAAA,EAAM;AAAA,EACb;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBS,aAAA,GAA+B;AACtC,IAAA,OAAO,MAAM,aAAA,EAAc;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAeA,iBAAA,GAA4B;AAC1B,IAAA,OAAO,IAAA,CAAK,SAAA,CAAU,IAAA,CAAK,aAAA,EAAe,CAAA;AAAA,EAC5C;AACF","file":"index.js","sourcesContent":["/**\n * Framework-agnostic SlopitRecorder implementation.\n *\n * @module\n */\n\nimport { BaseRecorder, type TrialConfig, discoverElement } from \"@slopit/adapter-shared\";\nimport type { PlatformInfo, ResponseInfo, SlopitSession, StimulusInfo } from \"@slopit/core\";\n\nimport type { VanillaRecorderConfig, VanillaTrialConfig, VanillaTrialResult } from \"./types.js\";\n\n/**\n * Framework-agnostic SlopitRecorder for custom experiment implementations.\n *\n * This is a reference implementation that can be used directly or as a\n * template for building custom platform-specific adapters.\n *\n * @example\n * ```typescript\n * const recorder = new SlopitRecorder({\n * participantId: \"P001\",\n * studyId: \"study_1\",\n * });\n *\n * // Start a trial\n * recorder.startTrial({\n * trialId: \"trial_1\",\n * targetElement: document.querySelector(\"textarea\"),\n * });\n *\n * // Later, when trial ends\n * const result = recorder.endTrial(\"participant response text\");\n *\n * // At experiment end\n * const session = recorder.exportSession();\n * ```\n */\nexport class SlopitRecorder extends BaseRecorder {\n private platformName: string;\n private platformVersion: string;\n private discoveryCleanup: (() => void) | null = null;\n\n /**\n * Creates a new SlopitRecorder.\n *\n * @param config - Recorder configuration\n */\n constructor(config?: VanillaRecorderConfig) {\n super(config);\n this.platformName = config?.platformName ?? \"vanilla\";\n this.platformVersion = config?.platformVersion ?? \"1.0.0\";\n }\n\n /**\n * Gets platform-specific information.\n *\n * @returns Platform information for session export\n */\n protected override getPlatformInfo(): PlatformInfo {\n return {\n name: this.platformName,\n version: this.platformVersion,\n adapterVersion: \"0.1.0\",\n };\n }\n\n /**\n * Normalizes a VanillaTrialConfig to a TrialConfig.\n *\n * @param config - Vanilla trial config with optional string stimulus\n * @returns Trial config compatible with base class\n */\n private normalizeTrialConfig(config: VanillaTrialConfig): TrialConfig {\n // Extract stimulus and convert string to StimulusInfo\n const stimulus: StimulusInfo | undefined =\n typeof config.stimulus === \"string\"\n ? { type: \"text\", content: config.stimulus }\n : config.stimulus;\n\n // Build the trial config explicitly, only including defined properties\n const trialConfig: TrialConfig = {};\n\n if (config.trialId !== undefined) {\n trialConfig.trialId = config.trialId;\n }\n if (config.trialIndex !== undefined) {\n trialConfig.trialIndex = config.trialIndex;\n }\n if (config.trialType !== undefined) {\n trialConfig.trialType = config.trialType;\n }\n if (config.targetElement !== undefined) {\n trialConfig.targetElement = config.targetElement;\n }\n if (config.targetSelector !== undefined) {\n trialConfig.targetSelector = config.targetSelector;\n }\n if (stimulus !== undefined) {\n trialConfig.stimulus = stimulus;\n }\n\n return trialConfig;\n }\n\n /**\n * Start capturing behavioral data for a trial.\n *\n * @param config - Trial configuration\n * @throws Error if a trial is already in progress\n *\n * @example\n * ```typescript\n * recorder.startTrial({\n * trialId: \"trial_1\",\n * targetElement: document.querySelector(\"textarea\"),\n * stimulus: \"Please describe your experience.\",\n * });\n * ```\n */\n override startTrial(config: VanillaTrialConfig): void {\n super.startTrial(this.normalizeTrialConfig(config));\n }\n\n /**\n * Start a trial with automatic element discovery.\n *\n * Use this when the target element may not exist yet (e.g., dynamically\n * rendered content). The method uses a MutationObserver to watch for\n * the element to appear in the DOM.\n *\n * @param config - Trial configuration (without targetElement)\n * @param selector - CSS selector for target element\n * @param container - Container to search within (default: document)\n * @param timeout - Max time to wait for element (default: 5000ms)\n * @returns Promise that resolves when the trial starts\n * @throws Error if element is not found within timeout\n *\n * @example\n * ```typescript\n * await recorder.startTrialWithDiscovery(\n * { trialId: \"trial_1\" },\n * \"textarea.response-input\",\n * document.body,\n * 3000\n * );\n * ```\n */\n startTrialWithDiscovery(\n config: VanillaTrialConfig,\n selector: string,\n container: HTMLElement | Document = document,\n timeout = 5000,\n ): Promise<void> {\n return new Promise((resolve, reject) => {\n // Try immediate discovery first\n const element = container.querySelector(selector) as HTMLElement | null;\n if (element !== null) {\n this.startTrial({ ...config, targetElement: element });\n resolve();\n return;\n }\n\n // Use MutationObserver for delayed discovery\n this.discoveryCleanup = discoverElement({\n selector,\n container: container as HTMLElement,\n timeout,\n onFound: (el) => {\n this.discoveryCleanup = null;\n this.startTrial({ ...config, targetElement: el });\n resolve();\n },\n onTimeout: () => {\n this.discoveryCleanup = null;\n // Start trial anyway without element\n this.startTrial(config);\n reject(new Error(`Element \"${selector}\" not found within ${timeout}ms`));\n },\n });\n });\n }\n\n /**\n * End the current trial and return captured data.\n *\n * This method accepts either a string or ResponseInfo for convenience.\n * Strings are automatically converted to text responses with character\n * and word counts.\n *\n * @param response - The participant's response (text string or ResponseInfo)\n * @returns The completed trial with behavioral data\n * @throws Error if no trial is in progress\n *\n * @example\n * ```typescript\n * // With a simple text response\n * const result = recorder.endTrial(\"The participant's typed response\");\n *\n * // With a ResponseInfo object\n * const result = recorder.endTrial({\n * type: \"text\",\n * value: \"Response text\",\n * characterCount: 13,\n * wordCount: 2,\n * });\n * ```\n */\n override endTrial(\n response?: string | ResponseInfo,\n platformData?: Record<string, unknown>,\n ): VanillaTrialResult {\n // Clean up any pending discovery\n if (this.discoveryCleanup !== null) {\n this.discoveryCleanup();\n this.discoveryCleanup = null;\n }\n\n // Normalize response: convert string to ResponseInfo\n let responseInfo: ResponseInfo | undefined;\n if (typeof response === \"string\") {\n responseInfo = {\n type: \"text\",\n value: response,\n characterCount: response.length,\n wordCount: response\n .trim()\n .split(/\\s+/)\n .filter((w) => w.length > 0).length,\n };\n } else if (response !== undefined) {\n responseInfo = response;\n }\n\n // Call the base class endTrial\n const trial = super.endTrial(responseInfo, platformData);\n\n return {\n trialId: trial.trialId,\n trialIndex: trial.trialIndex,\n trialType: trial.trialType,\n startTime: trial.startTime,\n endTime: trial.endTime,\n data: {\n behavioral: trial.behavioral ?? {\n keystrokes: [],\n focus: [],\n paste: [],\n mouse: [],\n scroll: [],\n clipboard: [],\n inputDuration: [],\n metrics: {},\n },\n flags: trial.captureFlags ?? [],\n },\n rt: trial.endTime - trial.startTime,\n };\n }\n\n /**\n * Check if a trial is currently in progress.\n *\n * @returns True if a trial is active\n *\n * @example\n * ```typescript\n * if (recorder.isTrialActive()) {\n * recorder.endTrial(responseText);\n * }\n * ```\n */\n isTrialActive(): boolean {\n return this.isTrialInProgress();\n }\n\n /**\n * Get the current trial ID, or null if no trial is active.\n *\n * @returns The current trial ID or null\n *\n * @example\n * ```typescript\n * const trialId = recorder.getCurrentTrialId();\n * if (trialId !== null) {\n * console.log(`Recording trial: ${trialId}`);\n * }\n * ```\n */\n getCurrentTrialId(): string | null {\n const trial = this.getCurrentTrial();\n return trial?.trialId ?? null;\n }\n\n /**\n * Get the number of completed trials.\n *\n * @returns The count of completed trials\n *\n * @example\n * ```typescript\n * console.log(`Completed ${recorder.getTrialCount()} trials`);\n * ```\n */\n getTrialCount(): number {\n return this.getTrials().length;\n }\n\n /**\n * Reset the recorder for a new session.\n *\n * Clears all trials, detaches any active capture, and generates a new\n * session ID. Use this when starting a new experiment session with the\n * same participant.\n *\n * @example\n * ```typescript\n * // After completing a session\n * const session = recorder.exportSession();\n * await saveSession(session);\n *\n * // Reset for a new session\n * recorder.resetSession();\n * ```\n */\n resetSession(): void {\n // Clean up pending discovery\n if (this.discoveryCleanup !== null) {\n this.discoveryCleanup();\n this.discoveryCleanup = null;\n }\n this.reset();\n }\n\n /**\n * Export the session data.\n *\n * @returns Complete session data in SlopitSession format\n *\n * @example\n * ```typescript\n * const session = recorder.exportSession();\n * await fetch(\"/api/sessions\", {\n * method: \"POST\",\n * headers: { \"Content-Type\": \"application/json\" },\n * body: JSON.stringify(session),\n * });\n * ```\n */\n override exportSession(): SlopitSession {\n return super.exportSession();\n }\n\n /**\n * Export session data as a JSON string.\n *\n * Convenience method for storing or transmitting session data.\n *\n * @returns JSON string representation of the session\n *\n * @example\n * ```typescript\n * const json = recorder.exportSessionJSON();\n * localStorage.setItem(\"session\", json);\n * ```\n */\n exportSessionJSON(): string {\n return JSON.stringify(this.exportSession());\n }\n}\n\n// Also export as VanillaRecorder for clarity\nexport { SlopitRecorder as VanillaRecorder };\n"]}
1
+ {"version":3,"sources":["../src/recorder.ts"],"names":[],"mappings":";;;AAqCO,IAAM,cAAA,GAAN,cAA6B,YAAA,CAAa;AAAA,EACvC,YAAA;AAAA,EACA,eAAA;AAAA,EACA,gBAAA,GAAwC,IAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOhD,YAAY,MAAA,EAAgC;AAC1C,IAAA,KAAA,CAAM,MAAM,CAAA;AACZ,IAAA,IAAA,CAAK,YAAA,GAAe,QAAQ,YAAA,IAAgB,SAAA;AAC5C,IAAA,IAAA,CAAK,eAAA,GAAkB,QAAQ,eAAA,IAAmB,OAAA;AAAA,EACpD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOmB,eAAA,GAAgC;AACjD,IAAA,OAAO;AAAA,MACL,MAAM,IAAA,CAAK,YAAA;AAAA,MACX,SAAS,IAAA,CAAK,eAAA;AAAA,MACd,cAAA,EAAgB;AAAA,KAClB;AAAA,EACF;AAAA;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\n * Framework-agnostic SlopitRecorder implementation.\n *\n * @module\n */\n\nimport { BaseRecorder, discoverElement, type TrialConfig } from \"@slopit/adapter-shared\";\nimport type { PlatformInfo, ResponseInfo, SlopitSession, StimulusInfo } from \"@slopit/core\";\n\nimport type { VanillaRecorderConfig, VanillaTrialConfig, VanillaTrialResult } from \"./types.js\";\n\n/**\n * Framework-agnostic SlopitRecorder for custom experiment implementations.\n *\n * This is a reference implementation that can be used directly or as a\n * template for building custom platform-specific adapters.\n *\n * @example\n * ```typescript\n * const recorder = new SlopitRecorder({\n * participantId: \"P001\",\n * studyId: \"study_1\",\n * });\n *\n * // Start a trial\n * recorder.startTrial({\n * trialId: \"trial_1\",\n * targetElement: document.querySelector(\"textarea\"),\n * });\n *\n * // Later, when trial ends\n * const result = recorder.endTrial(\"participant response text\");\n *\n * // At experiment end\n * const session = recorder.exportSession();\n * ```\n */\nexport class SlopitRecorder extends BaseRecorder {\n private platformName: string;\n private platformVersion: string;\n private discoveryCleanup: (() => void) | null = null;\n\n /**\n * Creates a new SlopitRecorder.\n *\n * @param config - Recorder configuration\n */\n constructor(config?: VanillaRecorderConfig) {\n super(config);\n this.platformName = config?.platformName ?? \"vanilla\";\n this.platformVersion = config?.platformVersion ?? \"1.0.0\";\n }\n\n /**\n * Gets platform-specific information.\n *\n * @returns Platform information for session export\n */\n protected override getPlatformInfo(): PlatformInfo {\n return {\n name: this.platformName,\n version: this.platformVersion,\n adapterVersion: \"0.1.0\",\n };\n }\n\n /**\n * Normalizes a VanillaTrialConfig to a TrialConfig.\n *\n * @param config - Vanilla trial config with optional string stimulus\n * @returns Trial config compatible with base class\n */\n private normalizeTrialConfig(config: VanillaTrialConfig): TrialConfig {\n // Extract stimulus and convert string to StimulusInfo\n const stimulus: StimulusInfo | undefined =\n typeof config.stimulus === \"string\"\n ? { type: \"text\", content: config.stimulus }\n : config.stimulus;\n\n // Build the trial config explicitly, only including defined properties\n const trialConfig: TrialConfig = {};\n\n if (config.trialId !== undefined) {\n trialConfig.trialId = config.trialId;\n }\n if (config.trialIndex !== undefined) {\n trialConfig.trialIndex = config.trialIndex;\n }\n if (config.trialType !== undefined) {\n trialConfig.trialType = config.trialType;\n }\n if (config.targetElement !== undefined) {\n trialConfig.targetElement = config.targetElement;\n }\n if (config.targetSelector !== undefined) {\n trialConfig.targetSelector = config.targetSelector;\n }\n if (stimulus !== undefined) {\n trialConfig.stimulus = stimulus;\n }\n\n return trialConfig;\n }\n\n /**\n * Start capturing behavioral data for a trial.\n *\n * @param config - Trial configuration\n * @throws Error if a trial is already in progress\n *\n * @example\n * ```typescript\n * recorder.startTrial({\n * trialId: \"trial_1\",\n * targetElement: document.querySelector(\"textarea\"),\n * stimulus: \"Please describe your experience.\",\n * });\n * ```\n */\n override startTrial(config: VanillaTrialConfig): void {\n super.startTrial(this.normalizeTrialConfig(config));\n }\n\n /**\n * Start a trial with automatic element discovery.\n *\n * Use this when the target element may not exist yet (e.g., dynamically\n * rendered content). The method uses a MutationObserver to watch for\n * the element to appear in the DOM.\n *\n * @param config - Trial configuration (without targetElement)\n * @param selector - CSS selector for target element\n * @param container - Container to search within (default: document)\n * @param timeout - Max time to wait for element (default: 5000ms)\n * @returns Promise that resolves when the trial starts\n * @throws Error if element is not found within timeout\n *\n * @example\n * ```typescript\n * await recorder.startTrialWithDiscovery(\n * { trialId: \"trial_1\" },\n * \"textarea.response-input\",\n * document.body,\n * 3000\n * );\n * ```\n */\n startTrialWithDiscovery(\n config: VanillaTrialConfig,\n selector: string,\n container: HTMLElement | Document = document,\n timeout = 5000,\n ): Promise<void> {\n return new Promise((resolve, reject) => {\n // Try immediate discovery first\n const element = container.querySelector(selector) as HTMLElement | null;\n if (element !== null) {\n this.startTrial({ ...config, targetElement: element });\n resolve();\n return;\n }\n\n // Use MutationObserver for delayed discovery\n this.discoveryCleanup = discoverElement({\n selector,\n container: container as HTMLElement,\n timeout,\n onFound: (el) => {\n this.discoveryCleanup = null;\n this.startTrial({ ...config, targetElement: el });\n resolve();\n },\n onTimeout: () => {\n this.discoveryCleanup = null;\n // Start trial anyway without element\n this.startTrial(config);\n reject(new Error(`Element \"${selector}\" not found within ${timeout}ms`));\n },\n });\n });\n }\n\n /**\n * End the current trial and return captured data.\n *\n * This method accepts either a string or ResponseInfo for convenience.\n * Strings are automatically converted to text responses with character\n * and word counts.\n *\n * @param response - The participant's response (text string or ResponseInfo)\n * @returns The completed trial with behavioral data\n * @throws Error if no trial is in progress\n *\n * @example\n * ```typescript\n * // With a simple text response\n * const result = recorder.endTrial(\"The participant's typed response\");\n *\n * // With a ResponseInfo object\n * const result = recorder.endTrial({\n * type: \"text\",\n * value: \"Response text\",\n * characterCount: 13,\n * wordCount: 2,\n * });\n * ```\n */\n override endTrial(\n response?: string | ResponseInfo,\n platformData?: Record<string, unknown>,\n ): VanillaTrialResult {\n // Clean up any pending discovery\n if (this.discoveryCleanup !== null) {\n this.discoveryCleanup();\n this.discoveryCleanup = null;\n }\n\n // Normalize response: convert string to ResponseInfo\n let responseInfo: ResponseInfo | undefined;\n if (typeof response === \"string\") {\n responseInfo = {\n type: \"text\",\n value: response,\n characterCount: response.length,\n wordCount: response\n .trim()\n .split(/\\s+/)\n .filter((w) => w.length > 0).length,\n };\n } else if (response !== undefined) {\n responseInfo = response;\n }\n\n // Call the base class endTrial\n const trial = super.endTrial(responseInfo, platformData);\n\n return {\n trialId: trial.trialId,\n trialIndex: trial.trialIndex,\n trialType: trial.trialType,\n startTime: trial.startTime,\n endTime: trial.endTime,\n data: {\n behavioral: trial.behavioral ?? {\n keystrokes: [],\n focus: [],\n paste: [],\n mouse: [],\n scroll: [],\n clipboard: [],\n inputDuration: [],\n metrics: {},\n },\n flags: trial.captureFlags ?? [],\n },\n rt: trial.endTime - trial.startTime,\n };\n }\n\n /**\n * Check if a trial is currently in progress.\n *\n * @returns True if a trial is active\n *\n * @example\n * ```typescript\n * if (recorder.isTrialActive()) {\n * recorder.endTrial(responseText);\n * }\n * ```\n */\n isTrialActive(): boolean {\n return this.isTrialInProgress();\n }\n\n /**\n * Get the current trial ID, or null if no trial is active.\n *\n * @returns The current trial ID or null\n *\n * @example\n * ```typescript\n * const trialId = recorder.getCurrentTrialId();\n * if (trialId !== null) {\n * console.log(`Recording trial: ${trialId}`);\n * }\n * ```\n */\n getCurrentTrialId(): string | null {\n const trial = this.getCurrentTrial();\n return trial?.trialId ?? null;\n }\n\n /**\n * Get the number of completed trials.\n *\n * @returns The count of completed trials\n *\n * @example\n * ```typescript\n * console.log(`Completed ${recorder.getTrialCount()} trials`);\n * ```\n */\n getTrialCount(): number {\n return this.getTrials().length;\n }\n\n /**\n * Reset the recorder for a new session.\n *\n * Clears all trials, detaches any active capture, and generates a new\n * session ID. Use this when starting a new experiment session with the\n * same participant.\n *\n * @example\n * ```typescript\n * // After completing a session\n * const session = recorder.exportSession();\n * await saveSession(session);\n *\n * // Reset for a new session\n * recorder.resetSession();\n * ```\n */\n resetSession(): void {\n // Clean up pending discovery\n if (this.discoveryCleanup !== null) {\n this.discoveryCleanup();\n this.discoveryCleanup = null;\n }\n this.reset();\n }\n\n /**\n * Export the session data.\n *\n * @returns Complete session data in SlopitSession format\n *\n * @example\n * ```typescript\n * const session = recorder.exportSession();\n * await fetch(\"/api/sessions\", {\n * method: \"POST\",\n * headers: { \"Content-Type\": \"application/json\" },\n * body: JSON.stringify(session),\n * });\n * ```\n */\n override exportSession(): SlopitSession {\n return super.exportSession();\n }\n\n /**\n * Export session data as a JSON string.\n *\n * Convenience method for storing or transmitting session data.\n *\n * @returns JSON string representation of the session\n *\n * @example\n * ```typescript\n * const json = recorder.exportSessionJSON();\n * localStorage.setItem(\"session\", json);\n * ```\n */\n exportSessionJSON(): string {\n return JSON.stringify(this.exportSession());\n }\n}\n\n// Also export as VanillaRecorder for clarity\nexport { SlopitRecorder as VanillaRecorder };\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slopit/adapter-vanilla",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Framework-agnostic adapter for slopit behavioral capture",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -39,14 +39,14 @@
39
39
  "dist"
40
40
  ],
41
41
  "dependencies": {
42
- "@slopit/adapter-shared": "0.1.0",
43
- "@slopit/behavioral": "0.1.0",
44
- "@slopit/core": "0.1.0"
42
+ "@slopit/adapter-shared": "0.1.1",
43
+ "@slopit/behavioral": "0.2.0",
44
+ "@slopit/core": "0.2.0"
45
45
  },
46
46
  "devDependencies": {
47
47
  "jsdom": "^25.0.1",
48
48
  "tsup": "^8.3.5",
49
- "typescript": "^5.7.2",
49
+ "typescript": "^5.9.3",
50
50
  "vitest": "^2.1.8"
51
51
  },
52
52
  "scripts": {