Skip to content

Commit

Permalink
Merge pull request #58 from Eureka-Shoulders/v1
Browse files Browse the repository at this point in the history
v1.0.0
  • Loading branch information
Sampaio Leal authored Jan 18, 2023
2 parents f1c16e8 + f97d05e commit c04eb9f
Show file tree
Hide file tree
Showing 27 changed files with 1,258 additions and 2,013 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

1 change: 0 additions & 1 deletion commitlint.config.js

This file was deleted.

66 changes: 27 additions & 39 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@euk-labs/fetchx",
"version": "0.5.9",
"types": "./lib/types/index.d.ts",
"main": "./lib/cjs/index.js",
"version": "1.0.0",
"types": "./lib/index.d.ts",
"main": "./lib/index.js",
"exports": {
"import": "./lib/esm/index.js",
"import": "./lib/index.js",
"require": "./lib/cjs/index.js",
"default": "./lib/esm/index.js"
"default": "./lib/index.js"
},
"files": [
"lib",
Expand All @@ -29,47 +29,35 @@
"license": "MIT",
"sideEffects": false,
"scripts": {
"build": "rimraf lib && tsc && tsc --project tsconfig.esm.json",
"build": "rimraf lib && tsc && tsc --project tsconfig.cjs.json",
"lint": "tsc --noEmit && eslint src -c .eslintrc.json --ext tsx,ts",
"test": "vitest run",
"gen-docs": "typedoc --options ./typedoc.config.ts"
},
"devDependencies": {
"@commitlint/cli": "^16.2.1",
"@commitlint/config-conventional": "^16.2.1",
"@testing-library/react": "^12.1.2",
"@types/faker": "^5.5.9",
"@types/react": "^17.0.36",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"chokidar": "^3.5.2",
"cross-env": "^7.0.3",
"debug": "^4.3.3",
"esbuild": "^0.14.11",
"eslint": "^8.3.0",
"eslint-plugin-react": "^7.27.1",
"faker": "^5.5.3",
"husky": "^7.0.4",
"jsdom": "^20.0.1",
"miragejs": "^0.1.42",
"mobx": "^6.3.7",
"mobx-react-lite": "^3.2.2",
"postcss": "^8.4.0",
"prettier": "^2.4.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"@faker-js/faker": "^7.6.0",
"@testing-library/react": "^13.4.0",
"@types/react": "^18.0.26",
"@typescript-eslint/eslint-plugin": "^5.48.0",
"@typescript-eslint/parser": "^5.48.0",
"@vitest/coverage-c8": "^0.26.3",
"eslint": "^8.31.0",
"eslint-plugin-react": "^7.31.11",
"jsdom": "^20.0.3",
"miragejs": "^0.1.46",
"mobx": "^6.7.0",
"mobx-react-lite": "^3.4.0",
"prettier": "^2.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "^3.0.2",
"rollup": "^2.60.1",
"typedoc": "^0.22.10",
"typescript": "^4.5.2",
"vitest": "^0.24.0"
},
"dependencies": {
"axios": "^1.1.2"
"typedoc": "^0.23.23",
"typescript": "^4.9.4",
"vitest": "^0.26.3"
},
"peerDependencies": {
"mobx": ">=6.3.7",
"react": ">=17",
"react-dom": ">=17"
"mobx": ">=6.7.0",
"react": ">=18",
"react-dom": ">=18"
}
}
61 changes: 14 additions & 47 deletions src/EntityStore.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { makeAutoObservable } from 'mobx';
import Repository, { Identifier } from './Repository';

/**
* In contrast with the ListStore, EntityStores can only handle a single entity.
*
* It can be used to fetch the entity by an identifier, update the loaded entity and delete it.
*/
export default class EntityStore {
/**
* @param repository The {@link Repository} to use for fetch data
*/
import { Repository, Identifier } from './Repository';

// TODO: implement generic type for the entity
export class EntityStore {
loading = false;
data: unknown | null = null;
identifier: Identifier | null = null;

constructor(private repository: Repository) {
makeAutoObservable<EntityStore, 'repository'>(
this,
Expand All @@ -18,45 +15,18 @@ export default class EntityStore {
);
}

/**
* The loading state of the store. If it's true, the store is fetching data.
*/
loading = false;
/**
* The data that represents an entity.
*/
data: unknown | null = null;
/**
* The identifier of the entity.
*/
identifier: Identifier | null = null;

/**
* Change the loading state of the store.
* @param loading The loading state to set.
*/
setLoading(loading: boolean) {
this.loading = loading;
}

/**
* Change the data of the entity.
* @param data The data that represents an entity..
*/
setData(data: unknown | null) {
this.data = data;
}

/**
* Change the identifier of the entity.
*/
setIdentifier(identifier: Identifier) {
this.identifier = identifier;
}

/**
* Fetch the entity by the provided identifier.
*/
async fetch() {
if (!this.identifier) {
return console.warn("Can't fetch without an identifier");
Expand All @@ -75,11 +45,7 @@ export default class EntityStore {
}

// TODO: Implement update with different methods: patch and put
/**
* A method to update the entity.
* @param data The data to update.
*/
async update(data: unknown) {
async update(data: object) {
if (!this.identifier) {
return console.warn("Can't update without an identifier");
}
Expand All @@ -88,7 +54,11 @@ export default class EntityStore {

try {
const response = await this.repository.patch(this.identifier, data);
this.setData(response.data);

if (this.data) {
this.setData({ ...this.data, ...response.data });
}

this.setLoading(false);
return response.data;
} catch (error) {
Expand All @@ -97,9 +67,6 @@ export default class EntityStore {
}
}

/**
* A method to delete the entity.
*/
async delete() {
if (!this.identifier) {
return console.warn("Can't delete without an identifier");
Expand Down
Loading

0 comments on commit c04eb9f

Please sign in to comment.