Skip to content

Commit

Permalink
Documentation and codes have been updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
cemalgnlts committed Apr 24, 2023
1 parent 8e41b4f commit b81a740
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 38 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ yarn add @cemalgnlts/mailjs
const Mailjs = require("@cemalgnlts/mailjs");
const mailjs = new Mailjs();
mailjs.createOneAccount()
.then((account)=>{
console.log(account.data);
.then(account => {
console.log(account.data);
});

mailjs.on("ready" , ()=>console.log("Ready To Listen!"));
mailjs.on("arrive" , (msg)=>console.log(msg));
});
mailjs.on("ready" , () => console.log("Ready To Listen!"));
mailjs.on("arrive" , msg => console.log(msg));
```

- Nodejs (ESM)
Expand All @@ -56,7 +56,7 @@ Include these `scripts` in `html` page before using to include the extented poly
<script src="https://cdn.jsdelivr.net/gh/cemalgnlts/Mailjs@latest/eventsource.min.js"></script>

<script>
const mailjs = new Mailjs();
const mailjs = new Mailjs();
</script>

```
Expand All @@ -65,15 +65,15 @@ For more reference visit `/examples` directory.

# Documentation

Returns a Promise object after the function is called. If the request is sent correctly, `status` returns true. If it returns incorrect, the `status` will be false and the `message` in the data is also added.
Returns a Promise object after the function is called. If the request is sent correctly, `status` returns true. If it returns incorrect, the `status` will be false and the `message` in the data is also added. If there is no error, `status` always returns `true`.

A successfull response example:

```json
{
"status": true,
"message": "ok",
"data": ...
"data": {}
}
```

Expand All @@ -83,7 +83,7 @@ A failed response example:
{
"status": false,
"message": "Invalid credentials.",
"data": ...
"data": {}
}
```

Expand Down
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ declare class Mailjs {
/** Retrieve a domain by its id. */
getDomain(domainId: string): type.DomainResult;
/** open an eventlistener to messages and error */
on(event: string, callback: type.MessageCallback | type.EmptyCallback | type.ErrorCallback): void;
on(event: "seen" | "delete" | "arrive" | "error" | "open" | "ready", callback: type.MessageCallback | type.EmptyCallback | type.ErrorCallback): void;
/** Clears the events and safely closes eventlistener */
close(): void;
/** Gets all the Message resources of a given page. */
Expand Down
32 changes: 16 additions & 16 deletions dist/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
export declare type Methods = "GET" | "POST" | "DELETE" | "PATCH";
export declare type PromiseResult<T> = Promise<IResult<T>>;
export declare type RegisterResult = PromiseResult<IRegisterResult>;
export declare type LoginResult = PromiseResult<ILoginResult>;
export declare type DeleteResult = Promise<IDelete>;
export declare type AccountResult = PromiseResult<IAccountResult>;
export declare type DomainListResult = PromiseResult<IDomainResult[]>;
export declare type DomainResult = PromiseResult<IDomainResult>;
export declare type MessageListResult = PromiseResult<IMessagesResult[]>;
export declare type MessageResult = PromiseResult<IMessageResult>;
export declare type MessageSeenResult = PromiseResult<IMessageSeen>;
export declare type SourceResult = PromiseResult<ISourceResource>;
export declare type MessageCallback = (message: IMessagesResult) => void;
export declare type EmptyCallback = () => void;
export declare type ErrorCallback = (err: any) => void;
export type Methods = "GET" | "POST" | "DELETE" | "PATCH";
export type PromiseResult<T> = Promise<IResult<T>>;
export type RegisterResult = PromiseResult<IRegisterResult>;
export type LoginResult = PromiseResult<ILoginResult>;
export type DeleteResult = Promise<IDelete>;
export type AccountResult = PromiseResult<IAccountResult>;
export type DomainListResult = PromiseResult<IDomainResult[]>;
export type DomainResult = PromiseResult<IDomainResult>;
export type MessageListResult = PromiseResult<IMessagesResult[]>;
export type MessageResult = PromiseResult<IMessageResult>;
export type MessageSeenResult = PromiseResult<IMessageSeen>;
export type SourceResult = PromiseResult<ISourceResource>;
export type MessageCallback = (message: IMessagesResult) => void;
export type EmptyCallback = () => void;
export type ErrorCallback = (err: any) => void;
/** register() */
interface IRegisterResult {
id: string;
Expand Down Expand Up @@ -101,7 +101,7 @@ interface ISourceResource {
downloadUrl: string;
data: string;
}
export declare type CreateOneAccountResult = Promise<DomainResult | RegisterResult | LoginResult | {
export type CreateOneAccountResult = Promise<DomainResult | RegisterResult | LoginResult | {
status: boolean;
data: {
username: string;
Expand Down
2 changes: 1 addition & 1 deletion mailjs.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,42 +100,43 @@ class Mailjs {
// Message

/** open an eventlistener to messages and error */
on(event:string , callback: type.MessageCallback | type.EmptyCallback | type.ErrorCallback){

on(event: "seen" | "delete" | "arrive" | "error" | "open" | "ready", callback: type.MessageCallback | type.EmptyCallback | type.ErrorCallback) {
const allowedEvents = ["seen" , "delete" , "arrive" , "error" , "ready"];

// Checking if valid events
if(!allowedEvents.includes(event)){
if(!allowedEvents.includes(event)) {
return;
}

if(!this.listener){
if(!this.listener) {
this.listener = new EventSource(`${this.baseMercure}?topic=/accounts/${this.id}` , {
headers : {
headers: {
"Authorization" : `Bearer ${this.token}`,
}
});

for(let i=0;i<3;i++){
this.events[allowedEvents[i]] = (_data)=>{};
for(let i=0; i<3; i++){
this.events[allowedEvents[i]] = (_data) => {};
}

this.listener.on("message" , this.callback_);
}

if(event==="error" || event==="ready"){
if(event === "error" || event === "ready") {

if(event==="ready"){
if(event === "ready") {
event = "open"
}

this.listener.on(event , callback);
this.listener.on(event, callback);
return;
}

this.events[event] = callback;
}

/** Clears the events and safely closes eventlistener */
close(){
close() {
this.events = {};
this.listener.close();
this.listener = null;
Expand Down

0 comments on commit b81a740

Please sign in to comment.