From c700a399e4fe82eba63bcb40e06987e41592c31d Mon Sep 17 00:00:00 2001 From: thosakwe Date: Tue, 13 Dec 2016 11:35:35 -0500 Subject: [PATCH] More --- .babelrc | 4 ++ .gitignore | 50 ++++++++++++++++++++- .npmignore | 89 +++++--------------------------------- README.md | 2 +- lib/angel_client.dart | 8 +++- lib/base_angel_client.dart | 24 ++++++---- lib/io.dart | 2 +- package.json | 13 +++++- pubspec.yaml | 2 +- test/io_test.dart | 2 +- 10 files changed, 100 insertions(+), 96 deletions(-) diff --git a/.babelrc b/.babelrc index e69de29b..c56a9bf9 100644 --- a/.babelrc +++ b/.babelrc @@ -0,0 +1,4 @@ +{ + "presets": ["es2015"], + "plugins": ["add-module-exports"] +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index e67c11e2..a84c50a4 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,52 @@ pubspec.lock .idea lib/angel_client.js -*.sum \ No newline at end of file +*.sum + +# Logs +logs +*.log +npm-debug.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules +jspm_packages + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity diff --git a/.npmignore b/.npmignore index 82e61f78..754250b1 100644 --- a/.npmignore +++ b/.npmignore @@ -1,79 +1,10 @@ -# Created by .ignore support plugin (hsz.mobi) -### JetBrains template -# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm -# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 - -# User-specific stuff: -.idea/workspace.xml -.idea/tasks.xml - -# Sensitive or high-churn files: -.idea/dataSources/ -.idea/dataSources.ids -.idea/dataSources.xml -.idea/dataSources.local.xml -.idea/sqlDataSources.xml -.idea/dynamic.xml -.idea/uiDesigner.xml - -# Gradle: -.idea/gradle.xml -.idea/libraries - -# Mongo Explorer plugin: -.idea/mongoSettings.xml - -## File-based project format: -*.iws - -## Plugin-specific files: - -# IntelliJ -/out/ - -# mpeltonen/sbt-idea plugin -.idea_modules/ - -# JIRA plugin -atlassian-ide-plugin.xml - -# Crashlytics plugin (for Android Studio and IntelliJ) -com_crashlytics_export_strings.xml -crashlytics.properties -crashlytics-build.properties -fabric.properties -### Dart template -# See https://www.dartlang.org/tools/private-files.html - -# Files and directories created by pub - -# SDK 1.20 and later (no longer creates packages directories) -.packages -.pub/ -build/ - -# Older SDK versions -# (Include if the minimum SDK version specified in pubsepc.yaml is earlier than 1.20) -.project -.buildlog -**/packages/ - - -# Files created by dart2js -# (Most Dart developers will use pub build to compile Dart, use/modify these -# rules if you intend to use dart2js directly -# Convention is to use extension '.dart.js' for Dart compiled to Javascript to -# differentiate from explicit Javascript files) -*.dart.js -*.part.js -*.js.deps -*.js.map -*.info.json - -# Directory created by dartdoc -doc/api/ - -# Don't commit pubspec lock file -# (Library packages only! Remove pattern if developing an application package) -pubspec.lock - +.babelrc +.istanbul.yml +.travis.yml +.editorconfig +.idea/ +src/ +test/ +!lib/ +.github/ +coverage \ No newline at end of file diff --git a/README.md b/README.md index 0f8cd15b..4c1a36ec 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # angel_client -[![pub 1.0.0-dev+20](https://img.shields.io/badge/pub-1.0.0--dev+20-red.svg)](https://pub.dartlang.org/packages/angel_client) +[![pub 1.0.0-dev+21](https://img.shields.io/badge/pub-1.0.0--dev+21-red.svg)](https://pub.dartlang.org/packages/angel_client) ![build status](https://travis-ci.org/angel-dart/client.svg) Client library for the Angel framework. diff --git a/lib/angel_client.dart b/lib/angel_client.dart index d56816b3..0915bdb8 100644 --- a/lib/angel_client.dart +++ b/lib/angel_client.dart @@ -9,6 +9,12 @@ export 'package:angel_framework/src/http/angel_http_exception.dart'; /// A function that configures an [Angel] client in some way. typedef Future AngelConfigurer(Angel app); +/// A function that deserializes data received from the server. +/// +/// This is only really necessary in the browser, where `json_god` +/// doesn't work. +typedef AngelDeserializer(x); + /// Represents an Angel server that we are querying. abstract class Angel { String get authToken; @@ -29,7 +35,7 @@ abstract class Angel { await configurer(this); } - Service service(String path, {Type type}); + Service service(String path, {Type type, AngelDeserializer deserializer}); Future delete(String url, {Map headers}); diff --git a/lib/base_angel_client.dart b/lib/base_angel_client.dart index 191df0bd..d44a1ee4 100644 --- a/lib/base_angel_client.dart +++ b/lib/base_angel_client.dart @@ -128,9 +128,10 @@ abstract class BaseAngelClient extends Angel { } @override - Service service(String path, {Type type}) { + Service service(String path, {Type type, AngelDeserializer deserializer}) { String uri = path.toString().replaceAll(straySlashes, ""); - return new BaseAngelService(client, this, '$basePath/$uri'); + return new BaseAngelService(client, this, '$basePath/$uri', + deserializer: deserializer); } String _join(url) { @@ -179,8 +180,13 @@ class BaseAngelService extends Service { final Angel app; final String basePath; final http.BaseClient client; + final AngelDeserializer deserializer; - BaseAngelService(this.client, this.app, this.basePath); + BaseAngelService(this.client, this.app, this.basePath, {this.deserializer}); + + deserialize(x) { + return deserializer != null ? deserializer(x) : x; + } makeBody(x) { return JSON.encode(x); @@ -234,7 +240,7 @@ class BaseAngelService extends Service { throw failure(response); } - return json; + return json.map(deserialize).toList(); } catch (e, st) { throw failure(response, error: e, stack: st); } @@ -250,7 +256,7 @@ class BaseAngelService extends Service { throw failure(response); } - return JSON.decode(response.body); + return deserialize(JSON.decode(response.body)); } catch (e, st) { throw failure(response, error: e, stack: st); } @@ -266,7 +272,7 @@ class BaseAngelService extends Service { throw failure(response); } - return JSON.decode(response.body); + return deserialize(JSON.decode(response.body)); } catch (e, st) { throw failure(response, error: e, stack: st); } @@ -282,7 +288,7 @@ class BaseAngelService extends Service { throw failure(response); } - return JSON.decode(response.body); + return deserialize(JSON.decode(response.body)); } catch (e, st) { throw failure(response, error: e, stack: st); } @@ -298,7 +304,7 @@ class BaseAngelService extends Service { throw failure(response); } - return JSON.decode(response.body); + return deserialize(JSON.decode(response.body)); } catch (e, st) { throw failure(response, error: e, stack: st); } @@ -314,7 +320,7 @@ class BaseAngelService extends Service { throw failure(response); } - return JSON.decode(response.body); + return deserialize(JSON.decode(response.body)); } catch (e, st) { throw failure(response, error: e, stack: st); } diff --git a/lib/io.dart b/lib/io.dart index 9bd8d80d..24841479 100644 --- a/lib/io.dart +++ b/lib/io.dart @@ -13,7 +13,7 @@ class Rest extends BaseAngelClient { Rest(String path) : super(new http.Client(), path); @override - Service service(String path, {Type type}) { + Service service(String path, {Type type, AngelDeserializer deserializer}) { String uri = path.replaceAll(straySlashes, ""); return new RestService( client, this, "$basePath/$uri", T != dynamic ? T : type); diff --git a/package.json b/package.json index 1bd640b7..70867bee 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,15 @@ "name": "angel_client", "version": "1.0.0-dev", "description": "Client library for the Angel framework.", - "main": "lib/angel_client.js", + "main": "build/angel_client.js", + "jsnext:main": "lib/angel_client.js", "directories": { "test": "test" }, "scripts": { + "compile": "npm run dartdevc && babel -o build/angel_client.js lib/angel_client.js", + "dartdevc": "dartdevc --modules node -o lib/angel_client.js lib/angel_client.dart", + "prepublish": "npm run compile", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { @@ -22,5 +26,10 @@ "bugs": { "url": "https://github.com/angel-dart/angel_client/issues" }, - "homepage": "https://github.com/angel-dart/angel_client#readme" + "homepage": "https://github.com/angel-dart/angel_client#readme", + "devDependencies": { + "babel-cli": "^6.18.0", + "babel-plugin-add-module-exports": "^0.2.1", + "babel-preset-es2015": "^6.18.0" + } } diff --git a/pubspec.yaml b/pubspec.yaml index ef29353c..8669f398 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_client -version: 1.0.0-dev+20 +version: 1.0.0-dev+21 description: Client library for the Angel framework. author: Tobe O homepage: https://github.com/angel-dart/angel_client diff --git a/test/io_test.dart b/test/io_test.dart index 4de6c9fc..158af35e 100644 --- a/test/io_test.dart +++ b/test/io_test.dart @@ -23,7 +23,7 @@ main() { clientApp = new client.Rest(url); clientPostcards = clientApp.service("postcards"); - clientTypedPostcards = clientApp.service("postcards", type: Postcard); + clientTypedPostcards = clientApp.service("postcards", type: Postcard); }); tearDown(() async {