0.0.16 • Published 1 year ago

@libvue/laravel-orion-api v0.0.16

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

laravel-orion-api

license coverage npm (tag)

A Laravel Orion compatible repository-based Javascript http-client powered by axios.

Install

> npm install --save @libvue/laravel-orion-api

Setup

Create a BaseRepository

// BaseRepository.js
import { LaravelOrionAPI } from "@libvue/laravel-orion-api";

class BaseRepository extends LaravelOrionAPI {
    constructor() {
        // If you want to use a custom axios config, use super(yourAxiosConfig), else just use super()
        super();
        this.baseURL = `${import.meta.env.VITE_API_DOMAIN}`;
        this.path = "";
    }
}

export default BaseRepository;

Extend from this BaseRepository

// PostRepository.js
import BaseRepository from "./BaseRepository.js";

class PostRepository extends BaseRepository {
    constructor() {
        super();
        this.path = "/posts";
    }
}

export default PostRepository;

Use it in different styles

By Creating an Instance

// app.js
import PostRepository from "PostRepository.js";
const PostRepositoryInstance = new PostRepository();

// Search some posts
PostRepositoryInstance.search({ limit: 10, sort: "-id" }).then((data) => {
    console.log(data);
});

By using a builder function

// app.js
import PostRepository from "PostRepository.js";

// Search some posts
PostRepository.make().search({ limit: 10, sort: "-id" }).then((data) => {
    console.log(data);
});

By Importing an instance

// app.js
import PostRepository from "PostRepository.js"; // Should use `export default new PostRepository()`;

// Search some posts
PostRepository.search({ limit: 10, sort: "-id" }).then((data) => {
    console.log(data);
});

Constructor Configuration

variabledefaultdescription
baseURL/Sets the baseURL for that instance
path''Sets the relative path to the baseURL
autoAborttrueEnables aborting ongoing requests for all methods inside the instance

Request Methods

instance methodhttp-methodparametersRestricted data keys
indexGETdataincludes, aggregates
searchPOSTdatapage, limit, sort, filters, search, includes, aggregates
storePOSTdata, multipart-
showGETid, dataincludes, aggregates
updatePATCH, PUT or POSTid, data, multipart, method = 'PATCH'-
destroyDELETEid-
restorePOSTid-
batchStorePOSTdata, multipart-
batchUpdatePATCH or POSTdata-
batchDestroyDELETEdata-
batchRestorePOSTdata-
indexRelationGETid, relation, dataincludes, aggregates
searchRelationPOSTid, relation, datapage, limit, sort, filters, search, includes, aggregates
showRelationGETid, relation, relationId, dataincludes, aggregates
storeRelationPOSTid, relation, data, multipart-
updateRelationPATCH or POSTid, relation, relationId, data, multipart-
destroyRelationDELETEid, relation, relationId-
restoreRelationPOSTid, relation, relationId-
batchStoreRelationPOSTid, relation, data, multipart-
batchUpdateRelationPATCH or POSTid, relation, data, multipart-
batchDestroyRelationDELETEid, relation, data-
batchRestoreRelationPOSTid, relation, data-
syncPATCHid, relation, data-
togglePATCHid, relation, data-
attachPOSTid, relation, data-
detachDELETEid, relation, data-
pivotPATCHid, relation, relationId, data-
associatePOSTid, relation, relatedKey-
dissociateDELETEid, relation, relationId-
abort-methodName OR custom id-

Helper Methods

instance methoddescription
makeA builder function if you need to create an instance in the chain
withoutAutoAbortIf the instance has autoAbort enabled, you can disable this effect for methods that use this in the chain
withAutoAbortIf the instance has autoAbort disabled, you can enable this effect for methods that use this in the chain
withAbortIdChanges the default abort id (method name), so you can use Instance.abort(abortId) more precise.

In depth aborting

Default behaviour

By default autoAbort is set to true for the entire instance.

Note: This will only with work synchronous requests.

Example:

const UserRepositoryInstance = new UserRepository();

// First Call
UserRepositoryInstance.index().catch((e) => {
    console.log(e.code);                    // ERR_CANCELED
});
// Second call 
UserRepositoryInstance.index().catch((e) => {
    console.log(e.code);                    // ERR_CANCELED
});
// Third Call 
UserRepositoryInstance.index().then((result) => {
    console.log(result.status);             // 200
});

Disable autoAbort for entire instance

Disabling autoAbort will not auto-abort anything. Ongoing requests can still be canceled with the UserRepository.abort('method') or UserRepository.abort('id') methods.

Example:

class UserRepository extends LaravelOrionAPI {
    constructor(withDelay = false) {
        super();
        this.autoAbort = false;               // Disable autoAbort here
    }
}
const UserRepositoryInstance = new UserRepository();

// First Call 
UserRepositoryInstance.index().then((result) => {
    console.log(result.status)                // 200
});
// Second call 
UserRepositoryInstance.index().then((result) => {
    console.log(result.status)                // 200
});
// Third Call 
UserRepositoryInstance.index().then((result) => {
    console.log(result.status)                // 200
});

Disable autoAbort for a single method

Example:

const UserRepositoryInstance = new UserRepository();

// First Call
UserRepositoryInstance.withoutAutoAbort().index().then((result) => {
    console.log(result.status);             // 200
});
// Second call 
UserRepositoryInstance.withoutAutoAbort().index().then((result) => {
    console.log(result.status);             // 200
});
// Third Call 
UserRepositoryInstance.withoutAutoAbort().index().then((result) => {
    console.log(result.status);             // 200
});

Note: If you are using the index() for two different purposes simultaneously, you must use withAbortId() to avoid weird behaviour. See example below

Example:

// First Call
const UserRepositoryInstance = new UserRepository();

UserRepositoryInstance.withoutAutoAbort().index().then((result) => {
    console.log(result.status);             // 200
});
// Second call 
UserRepositoryInstance.withoutAutoAbort().index().then((result) => {
    console.log(result.status);             // 200
});
// Third Call 
UserRepositoryInstance.withAbortId('other-purpose').index().catch((e) => {
    console.log(e.code);                    // ERR_CANCELED 
});
// Fourth Call 
UserRepositoryInstance.withAbortId('other-purpose').index().then((result) => {
    console.log(result.status);             // 200 
});
0.0.15

1 year ago

0.0.16

1 year ago

0.0.10

1 year ago

0.0.11

1 year ago

0.0.12

1 year ago

0.0.13

1 year ago

0.0.14

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago

0.0.0

1 year ago