@figma-vars/hooks 3.0.0 → 3.1.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.
Files changed (44) hide show
  1. package/README.md +50 -0
  2. package/dist/api/fetcher.d.ts +32 -1
  3. package/dist/api/fetcher.d.ts.map +1 -1
  4. package/dist/api/index.d.ts +2 -0
  5. package/dist/api/index.d.ts.map +1 -1
  6. package/dist/api/mutator.d.ts +27 -1
  7. package/dist/api/mutator.d.ts.map +1 -1
  8. package/dist/contexts/FigmaVarsProvider.d.ts +2 -0
  9. package/dist/contexts/FigmaVarsProvider.d.ts.map +1 -1
  10. package/dist/core.cjs +1 -1
  11. package/dist/core.mjs +1 -1
  12. package/dist/hooks/index.d.ts +45 -0
  13. package/dist/hooks/index.d.ts.map +1 -1
  14. package/dist/hooks/useCollectionById.d.ts +31 -0
  15. package/dist/hooks/useCollectionById.d.ts.map +1 -0
  16. package/dist/hooks/useInvalidateVariables.d.ts +3 -0
  17. package/dist/hooks/useInvalidateVariables.d.ts.map +1 -1
  18. package/dist/hooks/useModesByCollection.d.ts +34 -0
  19. package/dist/hooks/useModesByCollection.d.ts.map +1 -0
  20. package/dist/hooks/usePublishedVariables.d.ts.map +1 -1
  21. package/dist/hooks/useVariableById.d.ts +31 -0
  22. package/dist/hooks/useVariableById.d.ts.map +1 -0
  23. package/dist/hooks/useVariables.d.ts.map +1 -1
  24. package/dist/index-5ZyKWuYv.cjs +1 -0
  25. package/dist/index-ClHLYVvu.js +142 -0
  26. package/dist/index.cjs +1 -1
  27. package/dist/index.mjs +250 -167
  28. package/dist/types/contexts.d.ts +1 -0
  29. package/dist/types/contexts.d.ts.map +1 -1
  30. package/dist/types/figma.d.ts +9 -1
  31. package/dist/types/figma.d.ts.map +1 -1
  32. package/dist/types/mutations.d.ts +14 -0
  33. package/dist/types/mutations.d.ts.map +1 -1
  34. package/dist/utils/errorHelpers.d.ts +46 -0
  35. package/dist/utils/errorHelpers.d.ts.map +1 -1
  36. package/dist/utils/index.d.ts +2 -1
  37. package/dist/utils/index.d.ts.map +1 -1
  38. package/dist/utils/swrKeys.d.ts +24 -0
  39. package/dist/utils/swrKeys.d.ts.map +1 -0
  40. package/dist/utils/typeGuards.d.ts +50 -0
  41. package/dist/utils/typeGuards.d.ts.map +1 -0
  42. package/package.json +1 -1
  43. package/dist/index-BIUpDTdr.cjs +0 -1
  44. package/dist/index-Cd4HQQHO.js +0 -94
@@ -1 +1 @@
1
- {"version":3,"file":"errorHelpers.d.ts","sourceRoot":"","sources":["../../src/utils/errorHelpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAEtE;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAK5D;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,OAAO,EACd,cAAc,SAAsB,GACnC,MAAM,CAQR;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAE1E"}
1
+ {"version":3,"file":"errorHelpers.d.ts","sourceRoot":"","sources":["../../src/utils/errorHelpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAEtE;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAK5D;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,OAAO,EACd,cAAc,SAAsB,GACnC,MAAM,CAQR;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAE1E;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAErD;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAK3D"}
@@ -19,5 +19,6 @@
19
19
  * @public
20
20
  */
21
21
  export { filterVariables } from './filterVariables';
22
- export { isFigmaApiError, getErrorStatus, getErrorMessage, hasErrorStatus, } from './errorHelpers';
22
+ export { isFigmaApiError, getErrorStatus, getErrorMessage, hasErrorStatus, isRateLimited, getRetryAfter, } from './errorHelpers';
23
+ export { isLocalVariablesResponse, isPublishedVariablesResponse, } from './typeGuards';
23
24
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,EACL,eAAe,EACf,cAAc,EACd,eAAe,EACf,cAAc,GACf,MAAM,oBAAoB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,EACL,eAAe,EACf,cAAc,EACd,eAAe,EACf,cAAc,EACd,aAAa,EACb,aAAa,GACd,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EACL,wBAAwB,EACxB,4BAA4B,GAC7B,MAAM,kBAAkB,CAAA"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Shared utilities for constructing SWR cache keys consistently across hooks.
3
+ *
4
+ * @remarks
5
+ * Centralizes key construction logic to prevent mismatches between fetch hooks
6
+ * and invalidation utilities. Ensures that invalidation keys match the keys
7
+ * used by useSWR in fetch hooks.
8
+ *
9
+ * @internal
10
+ */
11
+ /**
12
+ * Parameters for constructing a published variables SWR key.
13
+ */
14
+ export interface PublishedVariablesKeyParams {
15
+ /** Figma file key, or null if not available */
16
+ fileKey: string | null;
17
+ /** Figma Personal Access Token, or null if not available */
18
+ token: string | null;
19
+ /** Provider instance ID for fallback cache scoping */
20
+ providerId: string | undefined;
21
+ /** Whether fallback file is available */
22
+ hasFallback: boolean;
23
+ }
24
+ //# sourceMappingURL=swrKeys.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swrKeys.d.ts","sourceRoot":"","sources":["../../src/utils/swrKeys.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAgBH;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C,+CAA+C;IAC/C,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;IACtB,4DAA4D;IAC5D,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;IACpB,sDAAsD;IACtD,UAAU,EAAE,MAAM,GAAG,SAAS,CAAA;IAC9B,yCAAyC;IACzC,WAAW,EAAE,OAAO,CAAA;CACrB"}
@@ -0,0 +1,50 @@
1
+ import { LocalVariablesResponse, PublishedVariablesResponse } from '../types/figma';
2
+ /**
3
+ * Runtime type guard to check if data matches LocalVariablesResponse structure.
4
+ *
5
+ * @remarks
6
+ * Use this to validate fallback files or API responses at runtime before casting.
7
+ * Validates the essential structure: meta object with variableCollections and variables.
8
+ *
9
+ * @param data - The data to validate
10
+ * @returns `true` if data matches LocalVariablesResponse structure
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { isLocalVariablesResponse } from '@figma-vars/hooks';
15
+ *
16
+ * if (isLocalVariablesResponse(fallbackData)) {
17
+ * // Safe to use as LocalVariablesResponse
18
+ * } else {
19
+ * console.error('Invalid fallback file structure');
20
+ * }
21
+ * ```
22
+ *
23
+ * @public
24
+ */
25
+ export declare function isLocalVariablesResponse(data: unknown): data is LocalVariablesResponse;
26
+ /**
27
+ * Runtime type guard to check if data matches PublishedVariablesResponse structure.
28
+ *
29
+ * @remarks
30
+ * Use this to validate fallback files or API responses at runtime before casting.
31
+ * Validates the essential structure: meta object with variableCollections and variables.
32
+ *
33
+ * @param data - The data to validate
34
+ * @returns `true` if data matches PublishedVariablesResponse structure
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * import { isPublishedVariablesResponse } from '@figma-vars/hooks';
39
+ *
40
+ * if (isPublishedVariablesResponse(fallbackData)) {
41
+ * // Safe to use as PublishedVariablesResponse
42
+ * } else {
43
+ * console.error('Invalid fallback file structure');
44
+ * }
45
+ * ```
46
+ *
47
+ * @public
48
+ */
49
+ export declare function isPublishedVariablesResponse(data: unknown): data is PublishedVariablesResponse;
50
+ //# sourceMappingURL=typeGuards.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typeGuards.d.ts","sourceRoot":"","sources":["../../src/utils/typeGuards.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,sBAAsB,EACtB,0BAA0B,EAC3B,MAAM,aAAa,CAAA;AAEpB;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,OAAO,GACZ,IAAI,IAAI,sBAAsB,CA0BhC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,4BAA4B,CAC1C,IAAI,EAAE,OAAO,GACZ,IAAI,IAAI,0BAA0B,CA0BpC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@figma-vars/hooks",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Typed React hooks for managing Figma Variables, modes, tokens, and bindings via API.",
5
5
  "author": "Mark Learst",
6
6
  "license": "MIT",
@@ -1 +0,0 @@
1
- "use strict";var O=Object.defineProperty;var f=(t,e,E)=>e in t?O(t,e,{enumerable:!0,configurable:!0,writable:!0,value:E}):t[e]=E;var L=(t,e,E)=>f(t,typeof e!="symbol"?e+"":e,E);const n="https://api.figma.com",h=`${n}/v1/files`,p=t=>`/v1/files/${t}/variables/published`,S=t=>`/v1/files/${t}/variables`,D=t=>`${h}/${t}/variables/local`,d="application/json",I="X-FIGMA-TOKEN",i="A Figma API token is required.",G=`${i} and file key are required.`,u="Failed to perform bulk update.",m="Failed to create Figma variable.",P="Failed to delete Figma variable.",N="Failed to update Figma variable.",l="An error occurred while fetching data from the Figma API.";class o extends Error{constructor(E,s){super(E);L(this,"statusCode");this.name="FigmaApiError",this.statusCode=s,Error.captureStackTrace&&Error.captureStackTrace(this,o)}}async function B(t,e){if(!e)throw new Error(i);const E=t.startsWith("http://")||t.startsWith("https://")?t:`${n}${t.startsWith("/")?"":"/"}${t}`,s=await fetch(E,{method:"GET",headers:{[I]:e,"Content-Type":d}});if(!s.ok){let A=l;const T=s.status;try{const a=s.headers.get("content-type");if(a!=null&&a.includes("application/json")){const _=await s.json();_!=null&&_.message?A=_.message:_!=null&&_.err&&(A=_.err)}}catch{}throw new o(A,T)}return s.json()}async function U(t,e,E,s){if(!e)throw new Error(i);const a={method:{CREATE:"POST",UPDATE:"PUT",DELETE:"DELETE"}[E],headers:{"Content-Type":"application/json",[I]:e}};s&&(a.body=JSON.stringify(s));const _=t.startsWith("http://")||t.startsWith("https://")?t:`${n}${t.startsWith("/")?"":"/"}${t}`,r=await fetch(_,a);if(!r.ok){const M=r.status;let R="An API error occurred";try{const c=r.headers.get("content-type");if(c!=null&&c.includes("application/json")){const F=await r.json();R=F.err||F.message||R}}catch{}throw new o(R,M)}return r.status===204||!r.body?{}:r.json()}function g(t,e){return t.filter(E=>{let s=!0;return e.resolvedType&&(s=s&&E.resolvedType===e.resolvedType),e.name&&(s=s&&E.name.includes(e.name)),s})}exports.CONTENT_TYPE_JSON=d;exports.ERROR_MSG_BULK_UPDATE_FAILED=u;exports.ERROR_MSG_CREATE_VARIABLE_FAILED=m;exports.ERROR_MSG_DELETE_VARIABLE_FAILED=P;exports.ERROR_MSG_FETCH_FIGMA_DATA_FAILED=l;exports.ERROR_MSG_TOKEN_FILE_KEY_REQUIRED=G;exports.ERROR_MSG_TOKEN_REQUIRED=i;exports.ERROR_MSG_UPDATE_VARIABLE_FAILED=N;exports.FIGMA_API_BASE_URL=n;exports.FIGMA_FILES_ENDPOINT=h;exports.FIGMA_FILE_VARIABLES_PATH=S;exports.FIGMA_LOCAL_VARIABLES_ENDPOINT=D;exports.FIGMA_PUBLISHED_VARIABLES_PATH=p;exports.FIGMA_TOKEN_HEADER=I;exports.FigmaApiError=o;exports.fetcher=B;exports.filterVariables=g;exports.mutator=U;
@@ -1,94 +0,0 @@
1
- var I = Object.defineProperty;
2
- var T = (t, s, a) => s in t ? I(t, s, { enumerable: !0, configurable: !0, writable: !0, value: a }) : t[s] = a;
3
- var l = (t, s, a) => T(t, typeof s != "symbol" ? s + "" : s, a);
4
- const A = "https://api.figma.com", F = `${A}/v1/files`, M = (t) => `/v1/files/${t}/variables/published`, O = (t) => `/v1/files/${t}/variables`, S = (t) => `${F}/${t}/variables/local`, u = "application/json", p = "X-FIGMA-TOKEN", h = "A Figma API token is required.", g = `${h} and file key are required.`, D = "Failed to perform bulk update.", G = "Failed to create Figma variable.", P = "Failed to delete Figma variable.", w = "Failed to update Figma variable.", m = "An error occurred while fetching data from the Figma API.";
5
- class E extends Error {
6
- constructor(a, e) {
7
- super(a);
8
- /** HTTP status code from the API response. */
9
- l(this, "statusCode");
10
- this.name = "FigmaApiError", this.statusCode = e, Error.captureStackTrace && Error.captureStackTrace(this, E);
11
- }
12
- }
13
- async function y(t, s) {
14
- if (!s)
15
- throw new Error(h);
16
- const a = t.startsWith("http://") || t.startsWith("https://") ? t : `${A}${t.startsWith("/") ? "" : "/"}${t}`, e = await fetch(a, {
17
- method: "GET",
18
- headers: {
19
- [p]: s,
20
- "Content-Type": u
21
- }
22
- });
23
- if (!e.ok) {
24
- let i = m;
25
- const d = e.status;
26
- try {
27
- const n = e.headers.get("content-type");
28
- if (n != null && n.includes("application/json")) {
29
- const o = await e.json();
30
- o != null && o.message ? i = o.message : o != null && o.err && (i = o.err);
31
- }
32
- } catch {
33
- }
34
- throw new E(i, d);
35
- }
36
- return e.json();
37
- }
38
- async function C(t, s, a, e) {
39
- if (!s)
40
- throw new Error(h);
41
- const n = {
42
- method: {
43
- CREATE: "POST",
44
- UPDATE: "PUT",
45
- DELETE: "DELETE"
46
- }[a],
47
- headers: {
48
- "Content-Type": "application/json",
49
- [p]: s
50
- }
51
- };
52
- e && (n.body = JSON.stringify(e));
53
- const o = t.startsWith("http://") || t.startsWith("https://") ? t : `${A}${t.startsWith("/") ? "" : "/"}${t}`, r = await fetch(o, n);
54
- if (!r.ok) {
55
- const f = r.status;
56
- let c = "An API error occurred";
57
- try {
58
- const _ = r.headers.get("content-type");
59
- if (_ != null && _.includes("application/json")) {
60
- const R = await r.json();
61
- c = R.err || R.message || c;
62
- }
63
- } catch {
64
- }
65
- throw new E(c, f);
66
- }
67
- return r.status === 204 || !r.body ? {} : r.json();
68
- }
69
- function N(t, s) {
70
- return t.filter((a) => {
71
- let e = !0;
72
- return s.resolvedType && (e = e && a.resolvedType === s.resolvedType), s.name && (e = e && a.name.includes(s.name)), e;
73
- });
74
- }
75
- export {
76
- u as C,
77
- h as E,
78
- O as F,
79
- g as a,
80
- M as b,
81
- E as c,
82
- N as d,
83
- A as e,
84
- y as f,
85
- F as g,
86
- S as h,
87
- p as i,
88
- D as j,
89
- G as k,
90
- P as l,
91
- C as m,
92
- w as n,
93
- m as o
94
- };