SanityConfig constructor

SanityConfig({
  1. required String projectId,
  2. required String dataset,
  3. String? token,
  4. String? apiVersion,
  5. bool? useCdn,
  6. Perspective? perspective,
  7. bool? explainQuery,
})

Creates a new Sanity configuration for fetching documents from a project and dataset

Implementation

SanityConfig({
  required this.projectId,
  required this.dataset,
  this.token,
  String? apiVersion,
  bool? useCdn,
  Perspective? perspective,
  bool? explainQuery,
})  : useCdn = useCdn ?? true,
      apiVersion = apiVersion ?? defaultApiVersion,
      perspective = perspective ?? Perspective.published,
      explainQuery = explainQuery ?? false {
  if (this.useCdn) {
    assert(this.perspective == Perspective.published,
        'When useCdn is true, perspective can only be set to published');
  } else {
    assert(
        this.perspective == Perspective.raw ||
            this.perspective == Perspective.drafts,
        'When useCdn is false, perspective must be either raw or drafts');
  }

  if (this.perspective != Perspective.published) {
    // Token is required for accessing draft content
    assert(token != null && token!.trim().isNotEmpty, '''
Invalid Token provided.
Setup an API token, with Viewer access, in the Sanity Management Console.
Without a valid token you will not be able to fetch data from Sanity.''');
  }

  assert(RegExp(r'^v\d{4}-\d{2}-\d{2}$').hasMatch(this.apiVersion),
      'Invalid API version provided. It should follow the format `vYYYY-MM-DD`');
}