Edits history of script submission #12121 for ' Create/modify/delete variables (figma)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Figma = {
      token: string;
    };
    /**
     * Create/modify/delete variables
     * **This API is available to full members of Enterprise orgs with Editor seats.
     */
    export async function main(
      auth: Figma,
      file_key: string,
      body: {
        variableCollections?:
          | {
              action: "CREATE";
              id?: string;
              name: string;
              initialModeId?: string;
              hiddenFromPublishing?: false | true;
            }
          | {
              action: "UPDATE";
              id: string;
              name?: string;
              hiddenFromPublishing?: false | true;
            }
          | { action: "DELETE"; id: string }[];
        variableModes?:
          | {
              action: "CREATE";
              id?: string;
              name: string;
              variableCollectionId: string;
            }
          | {
              action: "UPDATE";
              id: string;
              name?: string;
              variableCollectionId: string;
            }
          | { action: "DELETE"; id: string }[];
        variables?:
          | {
              action: "CREATE";
              id?: string;
              name: string;
              variableCollectionId: string;
              resolvedType: "BOOLEAN" | "FLOAT" | "STRING" | "COLOR";
              description?: string;
              hiddenFromPublishing?: false | true;
              scopes?:
                | "ALL_SCOPES"
                | "TEXT_CONTENT"
                | "CORNER_RADIUS"
                | "WIDTH_HEIGHT"
                | "GAP"
                | "ALL_FILLS"
                | "FRAME_FILL"
                | "SHAPE_FILL"
                | "TEXT_FILL"
                | "STROKE_COLOR"
                | "STROKE_FLOAT"
                | "EFFECT_FLOAT"
                | "EFFECT_COLOR"
                | "OPACITY"
                | "FONT_FAMILY"
                | "FONT_STYLE"
                | "FONT_WEIGHT"
                | "FONT_SIZE"
                | "LINE_HEIGHT"
                | "LETTER_SPACING"
                | "PARAGRAPH_SPACING"
                | "PARAGRAPH_INDENT"[];
              codeSyntax?: { WEB?: string; ANDROID?: string; iOS?: string };
            }
          | {
              action: "UPDATE";
              id: string;
              name?: string;
              description?: string;
              hiddenFromPublishing?: false | true;
              scopes?:
                | "ALL_SCOPES"
                | "TEXT_CONTENT"
                | "CORNER_RADIUS"
                | "WIDTH_HEIGHT"
                | "GAP"
                | "ALL_FILLS"
                | "FRAME_FILL"
                | "SHAPE_FILL"
                | "TEXT_FILL"
                | "STROKE_COLOR"
                | "STROKE_FLOAT"
                | "EFFECT_FLOAT"
                | "EFFECT_COLOR"
                | "OPACITY"
                | "FONT_FAMILY"
                | "FONT_STYLE"
                | "FONT_WEIGHT"
                | "FONT_SIZE"
                | "LINE_HEIGHT"
                | "LETTER_SPACING"
                | "PARAGRAPH_SPACING"
                | "PARAGRAPH_INDENT"[];
              codeSyntax?: { WEB?: string; ANDROID?: string; iOS?: string };
            }
          | { action: "DELETE"; id: string }[];
        variableModeValues?: {
          variableId: string;
          modeId: string;
          value:
            | string
            | number
            | false
            | true
            | { r: number; g: number; b: number }
            | { r: number; g: number; b: number; a: number }
            | { type: "VARIABLE_ALIAS"; id: string };
        }[];
      },
    ) {
      const url = new URL(`https://api.figma.com/v1/files/${file_key}/variables`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago