Skip to content

SDK v4 #2

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

Merged
merged 53 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
9540b1d
remove future wrapper
xr0master Aug 25, 2023
200e815
update packages
xr0master May 25, 2024
067e266
set version 4
xr0master May 25, 2024
466033b
add new interfaces. Improve it
xr0master May 26, 2024
a570154
add store, optional publicKey
xr0master May 26, 2024
c25f8e2
fix validateParams type
xr0master May 26, 2024
90d3616
add validateLimitRateParams
xr0master May 26, 2024
42ed832
add isBlockedValueInParams
xr0master May 26, 2024
e2924f6
isLimitRateHit add
xr0master May 26, 2024
c6e4e18
add errors methods
xr0master Jun 9, 2024
e35dee5
init method
xr0master Jun 9, 2024
edffa26
init implemented
xr0master Jun 9, 2024
3c6b866
add send implemented
xr0master Jun 9, 2024
4c16eb8
add default export for emailjs
xr0master Jun 9, 2024
ed185cf
fix typos
xr0master Jun 9, 2024
ebdb5c7
send: fix initialization
xr0master Jun 9, 2024
55d11ed
fix EmailJSResponseStatus call
xr0master Jun 9, 2024
31a7726
add storageProvider property
xr0master Jun 9, 2024
3116c4b
send: fix typo
xr0master Jun 9, 2024
f3d90ac
fix return type
xr0master Jun 9, 2024
a0cd5f7
sendPost: fix client init
xr0master Jun 9, 2024
4bb5358
fix async / await
xr0master Jun 9, 2024
b073a24
fix EmailJSResponseStatus params
xr0master Jun 9, 2024
816ce38
refactor storage
xr0master Jun 10, 2024
3737b30
sendPost: use new store
xr0master Jun 10, 2024
c5cf9b4
the required params can be also null
xr0master Jun 10, 2024
1e9c06c
allow to merge data
xr0master Jun 10, 2024
bff2584
use instance instead of util funstion
xr0master Jun 10, 2024
edf091a
emailjs: improve export
xr0master Jun 10, 2024
e374df1
store can manage the default value by itself
xr0master Jun 10, 2024
62d08c0
merge option objects
xr0master Jun 10, 2024
59e6e12
export sub options
xr0master Jun 10, 2024
c44e7df
allow customize the client
xr0master Jun 10, 2024
65011e2
const data
xr0master Jun 10, 2024
209caf8
drop meta package use
xr0master Jun 10, 2024
8fbfcad
fix old tests for new code
xr0master Jun 10, 2024
886aca2
errors: test
xr0master Jun 11, 2024
7b4fc4e
test EmailJSResponseStatus model
xr0master Jun 11, 2024
f8acb96
test DefaultStorage model
xr0master Jun 11, 2024
f2b1c63
isBLockedValueInParams: fix the data condition
xr0master Jun 11, 2024
51208c9
add test
xr0master Jun 11, 2024
0bd25e6
LimitRate: add tests
xr0master Jun 13, 2024
9b099c9
add id validation and tests
xr0master Jun 13, 2024
c343a79
validateParams: add test
xr0master Jun 13, 2024
1106789
remove unnesery async
xr0master Jun 14, 2024
89f552b
test methods
xr0master Jun 14, 2024
d6db443
README: move to new version
xr0master Jun 14, 2024
1a729f6
fix the setter bug
xr0master Jun 14, 2024
8158ef5
add IT
xr0master Jun 14, 2024
2ed1cac
add tests
xr0master Jun 14, 2024
f2a8f7e
improve the example
xr0master Jun 15, 2024
01ac231
add new CHANGELOG version
xr0master Jun 15, 2024
9ad2822
fix code format
xr0master Jun 15, 2024
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ migrate_working_dir/
.packages
build/
coverage/
.flutter-*
CRIB.md
37 changes: 25 additions & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
## 4.0.0

- The flutter version of the SDK packages has been adjusted to global version 4. The SDK has been completely refactored. New data validation has been added to avoid wasting quota. A set of new tests has been added. Improved test code coverage.

- Added [blockList option](https://www.emailjs.com/docs/sdk/options/#blocklist) to block requests for unwanted variable values
- Added [limitrate option](https://www.emailjs.com/docs/sdk/options/#limitrate) to install the throttle. For persistence, the shared_preferences package is used.

## 1.3.0
* Fix closed client [issue #1](https://github.com/emailjs-com/emailjs-flutter/issues/1)

- Fix closed client [issue #1](https://github.com/emailjs-com/emailjs-flutter/issues/1)

## 1.2.1
* Fix the environment SDK version

- Fix the environment SDK version

## 1.2.0
* Update packages

- Update packages

## 1.1.0
* Improve README.md

- Improve README.md

## 1.0.0
* Add support for the private key
* Instead of the public key argument, the settings object is passed, where the public and private keys can be set.
Check out the documentation and examples.

- Add support for the private key
- Instead of the public key argument, the settings object is passed, where the public and private keys can be set.
Check out the documentation and examples.

## 0.0.3
* re-format examples code

- re-format examples code

## 0.0.2

* Score fixes
* Added examples
* Dart formatter
- Score fixes
- Added examples
- Dart formatter

## 0.0.1

* Official EmailJS SDK for Flutter
- Official EmailJS SDK for Flutter
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Use you EmailJS account for sending emails.
This is a flutter-only version, otherwise use
- [Browser SDK](https://www.npmjs.com/package/@emailjs/browser)
- [Node.js SDK](https://www.npmjs.com/package/@emailjs/nodejs)
- [React Native SDK](https://www.npmjs.com/package/@emailjs/react-native)
- [REST API](https://www.emailjs.com/docs/rest-api/send/)

## Links
Expand All @@ -18,7 +19,7 @@ This is a flutter-only version, otherwise use
## Intro

EmailJS helps to send emails directly from your code.
No large knowledge is required – just connect EmailJS to one of the supported
No server is required – just connect EmailJS to one of the supported
email services, create an email template, and use our SDK
to trigger an email.

Expand All @@ -44,47 +45,47 @@ through [Account:Security](https://dashboard.emailjs.com/admin/account/security)
**send email**

```dart
import package:emailjs/emailjs.dart
import package:emailjs/emailjs.dart as emailjs

Map<String, dynamic> templateParams = {
'name': 'James',
'notes': 'Check this out!'
};

try {
await EmailJS.send(
'<YOUR_SERVICE_ID>',
'<YOUR_TEMPLATE_ID>',
await emailjs.send(
'YOUR_SERVICE_ID',
'YOUR_TEMPLATE_ID',
templateParams,
const Options(
publicKey: '<YOUR_PUBLIC_KEY>',
privateKey: '<YOUR_PRIVATE_KEY>',
const emailjs.Options(
publicKey: 'YOUR_PUBLIC_KEY',
privateKey: 'YOUR_PRIVATE_KEY',
),
);
print('SUCCESS!');
} catch (error) {
print(error.toString());
print('$error');
}
```

**init (optional)**
```dart
import package:emailjs/emailjs.dart
import package:emailjs/emailjs.dart as emailjs

// set Public Key as global settings
EmailJS.init(const Options(
publicKey: '<YOUR_PUBLIC_KEY>',
privateKey: '<YOUR_PRIVATE_KEY>',
emailjs.init(const emailjs.Options(
publicKey: 'YOUR_PUBLIC_KEY',
privateKey: 'YOUR_PRIVATE_KEY',
));

try {
// send the email without dynamic variables
await EmailJS.send(
'<YOUR_SERVICE_ID>',
'<YOUR_TEMPLATE_ID>',
await emailjs.send(
'YOUR_SERVICE_ID',
'YOUR_TEMPLATE_ID',
);
print('SUCCESS!');
} catch (error) {
print(error.toString());
print('$error');
}
```
25 changes: 14 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:emailjs/emailjs.dart';
import 'package:emailjs/emailjs.dart' as emailjs;

void main() {
runApp(const App());
Expand Down Expand Up @@ -31,22 +31,25 @@ class HomePage extends StatefulWidget {
class _HomePageState extends State<HomePage> {
void _sendEmail() async {
try {
await EmailJS.send(
'<YOUR_SERVICE_ID>',
'<YOUR_TEMPLATE_ID>',
await emailjs.send(
'YOUR_SERVICE_ID',
'YOUR_TEMPLATE_ID',
{
'user_email': '[email protected]',
'to_email': '[email protected]',
'message': 'Hi',
},
const Options(
publicKey: '<YOUR_PUBLIC_KEY>',
privateKey: '<YOUR_PRIVATE_KEY>',
),
const emailjs.Options(
publicKey: 'YOUR_PUBLIC_KEY',
privateKey: 'YOUR_PRIVATE_KEY',
limitRate: const emailjs.LimitRate(
id: 'app',
throttle: 10000,
)),
);
print('SUCCESS!');
} catch (error) {
if (error is EmailJSResponseStatus) {
print('ERROR... ${error.status}: ${error.text}');
if (error is emailjs.EmailJSResponseStatus) {
print('ERROR... $error');
}
print(error.toString());
}
Expand Down
72 changes: 5 additions & 67 deletions lib/emailjs.dart
Original file line number Diff line number Diff line change
@@ -1,71 +1,9 @@
library emailjs;

import 'dart:convert';
import 'package:http/http.dart' as http;

import 'src/models/emailjs_response_status.dart';
import 'src/utils/validate_params.dart';
import 'src/api/send_json.dart';
import 'src/models/options.dart';

export 'src/models/emailjs_response_status.dart';
export 'src/models/storage_provider.dart';
export 'src/models/options.dart';

class EmailJS {
/// Public Key specified in the [init] method
static String _publicKey = '';

/// Private Key specified in the [init] method
static String? _privateKey;

/// API host specified in the [init] method
static String _host = 'api.emailjs.com';

/// HTTP Client specified in the [init] method
static http.Client? _httpClient;

/// Global configuration for EmailJS
///
/// Sets globally the EmailJS [options]
static void init(
Options options, [
String? host,
http.Client? customHttpClient,
]) {
EmailJS._publicKey = options.publicKey;
EmailJS._privateKey = options.privateKey;
EmailJS._host = host ?? 'api.emailjs.com';
EmailJS._httpClient = customHttpClient;
}

/// Sends the email through the [serviceID] using the ready-made [templateID].
///
/// It's possible to pass [templatePrams] dynamic variables,
/// and set the [options] for this call.
static Future<EmailJSResponseStatus> send(
String serviceID,
String templateID, [
Map<String, dynamic>? templatePrams,
Options? options,
]) async {
final pubKey = options?.publicKey ?? EmailJS._publicKey;
final prKey = options?.privateKey ?? EmailJS._privateKey;

validateParams(pubKey, serviceID, templateID);

final Map<String, dynamic> params = {
'lib_version': '1.3.0',
'user_id': pubKey,
'accessToken': prKey,
'service_id': serviceID,
'template_id': templateID,
'template_params': templatePrams,
};

return await sendJSON(
Uri.https(EmailJS._host, 'api/v1.0/email/send'),
json.encode(params),
EmailJS._httpClient,
);
}
}
export 'src/models/block_list.dart';
export 'src/models/limit_rate.dart';
export 'src/methods/init.dart';
export 'src/methods/send.dart';
32 changes: 0 additions & 32 deletions lib/src/api/send_json.dart

This file was deleted.

35 changes: 35 additions & 0 deletions lib/src/api/send_post.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:http/http.dart';
import '../store/store.dart';
import '../models/emailjs_response_status.dart';

/// sends JSON object via HTTPS POST
Future<EmailJSResponseStatus> sendPost(
String url,
String data, [
Client? client,
]) async {
client ??= Client();

try {
final response = await client.post(
Uri.https(store.host, url),
headers: {
'Content-Type': 'application/json',
},
body: data,
);

final responseStatus = EmailJSResponseStatus(
status: response.statusCode,
text: response.body,
);

if (response.statusCode == 200) {
return responseStatus;
} else {
throw responseStatus;
}
} finally {
client.close();
}
}
8 changes: 8 additions & 0 deletions lib/src/errors/blocked_email_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import '../models/emailjs_response_status.dart';

EmailJSResponseStatus blockedEmailError() {
return const EmailJSResponseStatus(
status: 403,
text: 'Forbidden',
);
}
8 changes: 8 additions & 0 deletions lib/src/errors/limit_rate_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import '../models/emailjs_response_status.dart';

EmailJSResponseStatus limitRateError() {
return const EmailJSResponseStatus(
status: 429,
text: 'Too Many Requests',
);
}
18 changes: 18 additions & 0 deletions lib/src/methods/init.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:http/http.dart';
import '../models/options.dart';
import '../store/store.dart';

/// Global configuration for EmailJS
///
/// Sets globally the EmailJS [options]
void init(Options options, [Client? client]) {
store.publicKey = options.publicKey;
store.privateKey = options.privateKey;
store.blockList = options.blockList;
store.limitRate = options.limitRate;
store.storageProvider = options.storageProvider;
store.origin = options.origin;

// for testing porpose
store.client = client;
}
Loading
Loading