skill3th_core 0.1.0
skill3th_core: ^0.1.0 copied to clipboard
Skill3 flutter core project
skill3th_core #
Flutter client ของ Skill3 API: ยืนยัน site จากโดเมน, login พร้อมต่ออายุ token อัตโนมัติ และเรียก endpoint ใดก็ได้
ติดตั้ง #
dependencies:
skill3th_core: ^0.1.0
เริ่มใช้งาน #
สร้าง client ตัวเดียวทั้งแอป แล้วใช้ร่วมกัน
import 'package:skill3th_core/skill3th_core.dart';
final skill3 = createSkill3(
const Skill3Config(
scope: Skill3Scope.organ,
environment: Skill3Environment(
apiUrl: 'https://api.example.com',
defaultDomain: 'app.example.com',
),
),
);
await skill3.site.authDomain('app.example.com');
library ไม่อ่าน .env เอง ให้แอปอ่านค่าแล้วส่งผ่าน Skill3Config
Login #
final user = await skill3.auth.signInWithEmail('a@example.com', 'password');
final stream = await skill3.auth.subscribeUser(); // ได้ค่าปัจจุบันก่อน
stream.listen((user) => print(user == null ? 'signed out' : 'signed in'));
await skill3.auth.updateUser(); // โหลด profile ใหม่
await skill3.auth.signOut(); // ไม่มีเน็ตก็ล้าง session ในเครื่องได้
เมื่อ token หมดอายุ library ต่ออายุแล้วยิงซ้ำให้เองหนึ่งครั้ง
เรียก API #
final fetch = skill3.custom.fetch;
final Skill3List list = await fetch.get(
'land/areas',
query: {'limit': 20},
include: {'status': true},
);
print('${list.data} / ${list.total}');
final item = await fetch.get('land/areas', single: true, index: 7);
final res = await fetch.post('land/areas', data: {'name': 'a'});
print(res.status);
await fetch.post(
'land/areas/7/images',
data: {'userfiles': File(path)},
file: true,
);
| method | ผลลัพธ์ |
|---|---|
get |
Skill3List (data, total, headers) |
get กับ single: true |
body |
post, put, delete |
Skill3Response (status, headers, body, url) |
204 ไม่ใช่ error: list ว่าง หรือ body เป็น null
Error #
try {
await fetch.get('land/areas');
} on Skill3HttpError catch (err) {
if (err.status == 0) {
// ไม่มี response: NO_CONNECTION, REQUEST_TIMEOUT, SITE_NOT_READY
}
print(errorCode(err));
}
เก็บ session ที่อื่น #
implement StorageProvider (get, set, remove) แล้วส่งผ่าน Skill3Config.storage ค่าเริ่มต้นคือ SharedPreferences ซึ่งไม่เข้ารหัส
เอกสาร #
คู่มือย้ายจาก 0.0.x และเอกสารโครงสร้างอยู่ใน repo ของโปรเจกต์ ที่ docs/migration/0.0.x-to-0.1.md และ docs/ARCHITECTURE.md