Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for GET /variables/local & /variables/published endpoints #37

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ build/
# Directory created by dartdoc
doc/api/

# JetBrains
.idea/

.env
.envrc
9 changes: 9 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Figma Example

To run the example, you need to have a Figma API token. You can get it by following the instructions [here](https://www.figma.com/developers/docs#authentication).

This can be passed as a `define` variable to the `dart` command:

```bash
dart --define=FIGMA_TOKEN=<token> --define=FILE_KEY=<file_key> example.dart
```
4 changes: 2 additions & 2 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:figma/figma.dart';

void main() async {
final token = 'some_token';
final token = String.fromEnvironment('FIGMA_TOKEN');

final client = FigmaClient(token);
await client.getFile('some_file_key').then((res) => print(res.version));
await client.getFile(String.fromEnvironment('FILE_KEY')).then((res) => print(res.version));
}
17 changes: 17 additions & 0 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'dart:convert';
import 'dart:io';

import 'package:figma/figma.dart';
import 'package:figma/src/models/service/local_variables_response.dart';
import 'package:figma/src/models/service/published_variables_response.dart';
import 'package:http/http.dart';
import 'package:http2/http2.dart';

Expand Down Expand Up @@ -138,6 +140,20 @@ class FigmaClient {
_getFigma('/files/$key/styles', query)
.then((data) => StylesResponse.fromJson(data));

/// Retrieves all local variables from the Figma file specified by [key].
///
/// This API is only available to full members of Enterprise orgs.
Future<LocalVariablesResponse> getLocalVariables(String key) async =>
_getFigma('/files/$key/variables/local')
.then((data) => LocalVariablesResponse.fromJson(data));

/// Retrieves all published variables from the Figma file specified by [key].
///
/// This API is only available to full members of Enterprise orgs.
Future<PublishedVariablesResponse> getPublishedVariables(String key) async =>
_getFigma('/files/$key/variables/published')
.then((data) => PublishedVariablesResponse.fromJson(data));

/// Retrieves a specific style specified by [key].
Future<StyleResponse> getStyle(String key) async =>
_getFigma('/styles/$key').then((data) => StyleResponse.fromJson(data));
Expand Down Expand Up @@ -258,6 +274,7 @@ class FigmaClient {
class _Response {
final int statusCode;
final String body;

const _Response(this.statusCode, this.body);
}

Expand Down
6 changes: 6 additions & 0 deletions lib/src/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ export 'models/layout_grid.dart';
export 'models/layout_mode.dart';
export 'models/layout_pattern.dart';
export 'models/line.dart';
export 'models/local_variable.dart';
export 'models/local_variable_collection.dart';
export 'models/mode.dart';
export 'models/node.dart';
export 'models/overflow_direction.dart';
export 'models/paint.dart';
export 'models/primary_axis_align_items.dart';
export 'models/primary_axis_sizing_mode.dart';
export 'models/rectangle.dart';
export 'models/regular_polygon.dart';
export 'models/resolved_type.dart';
export 'models/size_rectangle.dart';
export 'models/slice.dart';
export 'models/star.dart';
Expand All @@ -44,6 +48,8 @@ export 'models/vector_2d.dart';
export 'models/vector.dart';
export 'models/comment.dart';
export 'models/user.dart';
export 'models/variable_code_syntax_platform.dart';
export 'models/variable_scope.dart';
export 'models/version.dart';
export 'models/project.dart';
export 'models/service/service.dart';
Expand Down
104 changes: 104 additions & 0 deletions lib/src/models/local_variable.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:equatable/equatable.dart';
import 'package:figma/src/models/resolved_type.dart';
import 'package:figma/src/models/variable_code_syntax_platform.dart';
import 'package:figma/src/models/variable_scope.dart';
import 'package:json_annotation/json_annotation.dart';

part 'local_variable.g.dart';

/// A response object containing a list of styles.
@JsonSerializable()
@CopyWith()
class LocalVariable extends Equatable {
/// The unique identifier of this variable.
final String id;

/// The name of this variable.
final String name;

/// The key of this variable.
final String key;

/// The id of the variable collection that contains this variable.
final String variableCollectionId;

/// The resolved type of the variable.
final ResolvedType resolvedType;

/// The values for each mode of this variable.
@JsonKey(defaultValue: {})
final Map<String, dynamic> valuesByMode;

/// Whether this variable is remote.
final bool remote;

/// The description of this variable.
final String description;

/// Whether this variable is hidden when publishing the current file as a library.
///
/// If the parent VariableCollection is marked as hiddenFromPublishing, then
/// this variable will also be hidden from publishing via the UI.
/// [hiddenFromPublishing] is independently toggled for a variable and collection.
/// However, both must be true for a given variable to be publishable.
final bool hiddenFromPublishing;

/// An array of scopes in the UI where this variable is shown.
///
/// Setting this property will show/hide this variable in the variable picker
/// UI for different fields.
///
/// Setting scopes for a variable does not prevent that variable from being
/// bound in other scopes (for example, via the Plugin API).
/// This only limits the variables that are shown in pickers within the Figma UI.
@JsonKey(defaultValue: [])
final List<VariableScope> scopes;

/// An object containing platform-specific code syntax definitions for a
/// variable. All platforms are optional.
@JsonKey(defaultValue: {})
final Map<VariableCodeSyntaxPlatform, String> codeSyntax;

final bool? deletedButReferenced;

/// A [LocalVariable] is a single design token that defines values for each of the
/// modes in its [LocalVariableCollection].
///
/// These values can be applied to various kinds of design properties.
LocalVariable({
required this.id,
required this.name,
required this.key,
required this.variableCollectionId,
required this.resolvedType,
required this.valuesByMode,
required this.remote,
required this.description,
required this.hiddenFromPublishing,
required this.scopes,
required this.codeSyntax,
this.deletedButReferenced,
});

@override
List<Object?> get props => [
id,
name,
key,
variableCollectionId,
resolvedType,
valuesByMode,
remote,
description,
hiddenFromPublishing,
scopes,
codeSyntax,
deletedButReferenced,
];

factory LocalVariable.fromJson(Map<String, dynamic> json) =>
_$LocalVariableFromJson(json);

Map<String, dynamic> toJson() => _$LocalVariableToJson(this);
}
Loading