Skip to content

Commit

Permalink
chore: unit tests and updated pubspec description
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanpodila committed Apr 5, 2024
1 parent 8fe3885 commit c48969e
Show file tree
Hide file tree
Showing 15 changed files with 378 additions and 47 deletions.
4 changes: 2 additions & 2 deletions apps/vyuh_demo/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ packages:
path: "../../packages/sanity/flutter_sanity_portable_text"
relative: true
source: path
version: "1.0.0-beta.8"
version: "1.0.0-beta.20"
flutter_shaders:
dependency: transitive
description:
Expand Down Expand Up @@ -645,7 +645,7 @@ packages:
path: "../../packages/system/vyuh_feature_system"
relative: true
source: path
version: "1.0.0-beta.4"
version: "1.0.0-beta.5"
web:
dependency: transitive
description:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: example
name: flutter_sanity_portable_text_example
description: A simple Flutter project to show the usage of flutter_sanity_portable_text
publish_to: none
version: 1.0.0+1
Expand Down
10 changes: 8 additions & 2 deletions packages/sanity/sanity_client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
## 1.0.0-beta.5

- Refactored tests for 100% coverage
- Updated pubspec description and readme
- Added documentation for all the classes and methods

## 1.0.0-beta.4

- Moved the sanity packages under the vyuh repo
- Moved the sanity packages under the vyuh repo

- **REFACTOR**: moving flutter_sanity_portable_text and sanity_client under vyuh. ([f1175fbd](https://github.com/vyuh-tech/vyuh/commit/f1175fbdb602588ef5f8d978a3d474f15a96e861))
- **REFACTOR**: moving flutter_sanity_portable_text and sanity_client under
vyuh.
([f1175fbd](https://github.com/vyuh-tech/vyuh/commit/f1175fbdb602588ef5f8d978a3d474f15a96e861))

## 1.0.0-beta.3

Expand Down
50 changes: 39 additions & 11 deletions packages/sanity/sanity_client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,45 @@ https://www.sanity.io/docs/http-query

- Connect to a Sanity.io project and run GROQ.
- Support all parameters allowed by the HTTP API of Sanity including
`perspective`, `explain`, `useCDN`, etc.
- Has support for switching between URL Builders for images. This is useful if
you are hosting your images on an external service like Cloudinary or ImageKit

## Getting started

Use the package by adding to your dependencies. It is still in Beta, so there
will be rough edges for sure.🤞
`apiVersion`, `perspective`, `explain`, `useCDN`.
- Has support for switching between URL Builders for images and files. This is
useful if you are hosting your images on an external service like _Cloudinary_
or _ImageKit_.

## Usage

//TODO

A more comprehensive example will be added along with rich documentation.
Create an instance of the SanityClient and give it a SanityConfig. Use the
`fetch` method to run queries.

```dart
import 'package:sanity_client/sanity_client.dart';
Future<void> main() async {
// using the SanityClient
var client = SanityClient(
SanityConfig(
projectId: 'your_project_id',
dataset: 'your_dataset',
token: 'your_token',
perspective: Perspective.published,
explainQuery: true,
useCdn: true,
apiVersion: 'v2024-02-16',
),
);
// make a query
var query = '''
*[_type == "movie"]{
_id,
title,
releaseDate,
"director": crewMembers[job == "Director"][0].person->name
}
''';
final response = await client.fetch(query);
print(response);
}
```
3 changes: 3 additions & 0 deletions packages/sanity/sanity_client/example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
1 change: 1 addition & 0 deletions packages/sanity/sanity_client/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Example for using the Sanity Client
29 changes: 29 additions & 0 deletions packages/sanity/sanity_client/example/lib/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:sanity_client/sanity_client.dart';

Future<void> main() async {
// using the SanityClient
var client = SanityClient(
SanityConfig(
projectId: 'your_project_id',
dataset: 'your_dataset',
token: 'your_token',
perspective: Perspective.published,
explainQuery: true,
useCdn: true,
apiVersion: 'v2024-02-16',
),
);

// make a query
var query = '''
*[_type == "movie"]{
_id,
title,
releaseDate,
"director": crewMembers[job == "Director"][0].person->name
}
''';

final response = await client.fetch(query);
print(response);
}
150 changes: 150 additions & 0 deletions packages/sanity/sanity_client/example/pubspec.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
async:
dependency: transitive
description:
name: async
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
url: "https://pub.dev"
source: hosted
version: "2.11.0"
characters:
dependency: transitive
description:
name: characters
sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605"
url: "https://pub.dev"
source: hosted
version: "1.3.0"
collection:
dependency: transitive
description:
name: collection
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
url: "https://pub.dev"
source: hosted
version: "1.18.0"
flutter:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: transitive
description:
name: http
sha256: "761a297c042deedc1ffbb156d6e2af13886bb305c2a343a4d972504cd67dd938"
url: "https://pub.dev"
source: hosted
version: "1.2.1"
http_parser:
dependency: transitive
description:
name: http_parser
sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b"
url: "https://pub.dev"
source: hosted
version: "4.0.2"
json_annotation:
dependency: transitive
description:
name: json_annotation
sha256: b10a7b2ff83d83c777edba3c6a0f97045ddadd56c944e1a23a3fdf43a1bf4467
url: "https://pub.dev"
source: hosted
version: "4.8.1"
lints:
dependency: "direct dev"
description:
name: lints
sha256: cbf8d4b858bb0134ef3ef87841abdf8d63bfc255c266b7bf6b39daa1085c4290
url: "https://pub.dev"
source: hosted
version: "3.0.0"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
url: "https://pub.dev"
source: hosted
version: "0.8.0"
meta:
dependency: transitive
description:
name: meta
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
url: "https://pub.dev"
source: hosted
version: "1.11.0"
path:
dependency: transitive
description:
name: path
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
url: "https://pub.dev"
source: hosted
version: "1.9.0"
sanity_client:
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "1.0.0-beta.5"
sky_engine:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
source_span:
dependency: transitive
description:
name: source_span
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
url: "https://pub.dev"
source: hosted
version: "1.10.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84
url: "https://pub.dev"
source: hosted
version: "1.2.1"
typed_data:
dependency: transitive
description:
name: typed_data
sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c
url: "https://pub.dev"
source: hosted
version: "1.3.2"
vector_math:
dependency: transitive
description:
name: vector_math
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
url: "https://pub.dev"
source: hosted
version: "2.1.4"
web:
dependency: transitive
description:
name: web
sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
sdks:
dart: ">=3.3.3 <4.0.0"
14 changes: 14 additions & 0 deletions packages/sanity/sanity_client/example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: sanity_client_example
description: An example for using the sanity_client package
version: 1.0.0
# repository: https://github.com/my_org/my_repo

environment:
sdk: ^3.3.3

# Add regular dependencies here.
dependencies:
sanity_client: ^1.0.0-beta.1

dev_dependencies:
lints: ^3.0.0
35 changes: 33 additions & 2 deletions packages/sanity/sanity_client/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@ import 'package:http/http.dart' as http;

import 'sanity_client.dart';

/// The various perspectives that can be used to fetch data from Sanity
enum Perspective { raw, previewDrafts, published }

/// Configuration for the Sanity client
final class SanityConfig {
/// The dataset to fetch data from
final String dataset;

/// The project id
final String projectId;

/// The token to use for authentication
final String token;

/// Whether to use the CDN or not
final bool useCdn;

/// The API version to use. It follows the format `vYYYY-MM-DD`
final String apiVersion;

/// The perspective to use
final Perspective perspective;

/// Whether to explain the query or not
final bool explainQuery;

/// The default API version to use
static final String defaultApiVersion = (() {
final today = DateTime.now();
final parts = [
Expand Down Expand Up @@ -42,12 +58,22 @@ final class SanityConfig {
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`');
}
}

/// The client for fetching data from Sanity
class SanityClient {
/// The configuration for the client
final SanityConfig config;

/// The HTTP client to use. Generally not needed to be provided.
/// It is used for testing purposes
final http.Client httpClient;

/// The URL builder to use. When not provided it uses the default Sanity URL builder
final UrlBuilder urlBuilder;

final Map<String, String> _requestHeaders;
Expand All @@ -60,16 +86,21 @@ class SanityClient {
urlBuilder = urlBuilder ?? SanityUrlBuilder(config),
_requestHeaders = {'Authorization': 'Bearer ${config.token}'};

Future<SanityQueryResponse> fetch(Uri uri) async {
/// Fetches data from Sanity by running the GROQ Query with the passed in parameters
Future<SanityQueryResponse> fetch(String query,
{Map<String, String>? params}) async {
final uri = queryUrl(query, params: params);
final response = await httpClient.get(uri, headers: _requestHeaders);

return _getQueryResult(response);
}

/// The URL for the query
Uri queryUrl(String query, {Map<String, String>? params}) =>
urlBuilder.queryUrl(query, params: params);

//ignore: long-parameter-list
/// Return the associated image url
//ignore: long-parameter-list
Uri imageUrl(
final String imageRefId, {
final int? width,
Expand Down
Loading

0 comments on commit c48969e

Please sign in to comment.