Skip to content
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

Feature/resource builder #2

Merged
merged 26 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
208892d
Create first saga handler for update action + start to create action …
Apr 22, 2018
0d4a72a
Handle read list resource and basic store it into redux store
Apr 29, 2018
4b85dc5
Add flow to project
Apr 29, 2018
3abd164
Add test stack
May 1, 2018
4867997
Change wording to better match data
May 1, 2018
3f43fc8
Add a test for readlist saga
May 1, 2018
af54843
Fix eslint recommandations
May 1, 2018
fbafb07
Add jsonapi resource reader with basic sort/filter/sideload operations
Startouf May 6, 2018
74c6dd7
Fix bad conception and implement paramsAsObject
Startouf May 6, 2018
e6b69da
Add code and tests for resource reads
Startouf May 31, 2018
040c4a6
Add jsonapi writer resources and circleci config
Startouf May 31, 2018
b01ebee
Update readme, add badges
Startouf May 31, 2018
d3154fd
Disable test watching for CI
Startouf May 31, 2018
3741ea9
Add better circleCI interface, skip some pending tests
Startouf May 31, 2018
1e57d17
Change write association linking DSL to better reflect toOne vs toMany
Startouf May 31, 2018
f57fa75
Implement review
Startouf Aug 2, 2018
c9ea94d
Fix eslint errors
Startouf Aug 4, 2018
6df0c7d
Update doc to handle collections
Startouf Aug 4, 2018
096bc51
Add URL stuff, improve existing code
Startouf Aug 4, 2018
00c099e
Handle API definitions
Startouf Aug 4, 2018
f9950ec
update API usage
Startouf Aug 4, 2018
55a9c43
WIP on review
Startouf Aug 7, 2018
de5883a
wip on review continued
Startouf Aug 7, 2018
8d9e9b3
Fix more ESLint errors
Startouf Aug 15, 2018
ac97b95
Add missing comment
Startouf Aug 15, 2018
e913e86
Implement review
Startouf Aug 15, 2018
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
10 changes: 10 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
"env",
"flow"
],
"plugins": [
"transform-runtime",
"transform-object-rest-spread"
]
}
41 changes: 41 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
- image: circleci/node:7.10

# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
# - image: circleci/mongo:3.4.4

working_directory: ~/repo

steps:
- checkout

# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-

- run: yarn install

- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}

# run tests!
- run:
name: Jest Suite
command: yarn jest tests --ci --testResultsProcessor="jest-junit"
environment:
JEST_JUNIT_OUTPUT: "reports/junit/js-test-results.xml"
7 changes: 6 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"extends": "airbnb-base"
"parser": "babel-eslint",
"extends": "airbnb-base",
"env": {
"browser": true,
"jest": true
}
}
11 changes: 11 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[ignore]

[include]

[libs]

[lints]

[options]

[strict]
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Jsonapi Orchestrator

Building better jsonapi-compliant code

[![BCH compliance](https://bettercodehub.com/edge/badge/MyJobGlasses/jsonapi-orchestrator?branch=master)](https://bettercodehub.com/)

[![CircleCI](https://circleci.com/gh/MyJobGlasses/jsonapi-orchestrator.svg?style=svg)](https://circleci.com/gh/MyJobGlasses/jsonapi-orchestrator)

# HOW TO

[See examples in the /examples folder](./examples/)

# TODO
[draft]
- think to add fetch polyfill
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je ne pense pas que ce soit necessaire, je pense qu'il faut plus préciser à l'utilisateur de le faire lui même s'il veut que ça fonctionne.
Raison : Si le dev a déjà installé le polyfill, on risque d'alourdir inutilement le package

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c'est ton code à toi que j'avais repris ça lol



21 changes: 18 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Jsonapi powered framework to harness the power of jsonapi with redux",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest --notify --watch"
},
"repository": {
"type": "git",
Expand All @@ -21,12 +21,27 @@
},
"homepage": "https://github.com/MyJobGlasses/jsonapi-orchestrator#readme",
"dependencies": {
"json-api-normalizer": "^0.4.10",
"lodash": "^4.17.10",
"redux": "^3.7.2",
"redux-saga": "^0.16.0"
"redux-saga": "^0.16.0",
"uuid": "^3.2.1"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.3",
"babel-jest": "^22.4.3",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1",
"babel-preset-flow": "^6.23.0",
"eslint": "^4.19.1",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.9.0"
"eslint-plugin-import": "^2.11.0",
"flow-bin": "^0.71.0",
"jest": "^22.4.3",
"jest-junit": "^4.0.0",
"redux-saga-test-plan": "^3.6.0",
"regenerator-runtime": "^0.11.1"
}
}
24 changes: 24 additions & 0 deletions src/JsonApiConnector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let mainConnector = null;

/**
* @todo handle if we want to overwrite mainConnector
*/
export default class JsonApiConnector {
constructor(params) {
this.url = params.url;
mainConnector = mainConnector || this;
}

static connector() {
if (!mainConnector) {
return new JsonApiConnector();
}
return mainConnector;
}

buildEndpoint(path) {
return `${this.url}/${path}`;
}
}

export const getMainConnector = JsonApiConnector.connector;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To delete ? Replaced by Api ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aaaah merde j'avais zappé ce truc là on avait parlé de connecteur a un moment mais je sais plus ce qu'on voulait y mettre dedans...

116 changes: 116 additions & 0 deletions src/__tests__/builders/JsonapiRequestBuilder.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import JsonapiRequestBuilder from '../../builders/JsonapiRequestBuilder';
import JsonapiResourceReader from '../../builders/JsonapiResourceReader';
import JsonapiResourceWriter from '../../builders/JsonapiResourceWriter';

describe('JsonapiRequestBuilder', () => {
let requestBuilder;
let resource;

describe('constructor', () => {
test('can assign requestBuilder variables during instanciation', () => {
resource = new JsonapiResourceReader();
const requestBuilderArguments = {
path: '/foo/bar',
httpMethod: 'OPTIONS',
resource,
params: { id: '42' },
};
requestBuilder = new JsonapiRequestBuilder(requestBuilderArguments);
expect(requestBuilder.path).toEqual('/foo/bar');
expect(requestBuilder.httpMethod).toEqual('OPTIONS');
expect(requestBuilder.resource).toEqual(resource);
expect(requestBuilder.params).toEqual(expect.objectContaining({ id: '42' }));
});
});

describe('instance methods', () => {
describe('#addMeta', () => {
beforeEach( () => {
requestBuilder = new JsonapiRequestBuilder({});
});

test('merges new meta', () => {
requestBuilder.addMeta({ invitationToken: '42' });
requestBuilder.addMeta({ token: 'deadbeef' });

expect(requestBuilder.meta).toMatchObject({
invitationToken: '42',
token: 'deadbeef',
});
});
});

describe('#action', () => {
describe('building a read request', () => {
beforeEach(() => {
resource = new JsonapiResourceReader({
jsonapiType: 'conversation', params: { id: 'cafebabe' },
});
requestBuilder = new JsonapiRequestBuilder({
resource, method: 'OPTIONS', path: '/conversations',
});
});

test('returns a correct read action', () => {
expect(requestBuilder.action()).toMatchObject({
type: 'READ_CONVERSATION_RESOURCE',
// params: { id: 'cafebabe' },
meta: {},
});
});
});

describe('building a write request', () => {
beforeEach(() => {
const conversationResource = new JsonapiResourceWriter({
jsonapiType: 'conversation',
params: { userid: 'cafebabe' },
attributes: {
recipient: 'deadbeef',
},
});
const messageResource = new JsonapiResourceWriter({
jsonapiType: 'message',
attributes: {
text: 'Hello world !',
clientId: '0ff1ce',
},
});
conversationResource.sidepost('messages', [messageResource]);
requestBuilder = new JsonapiRequestBuilder({
resource: conversationResource, path: '/conversations',
});
});

test('returns a correct write action', () => {
expect(requestBuilder.action()).toMatchObject({
type: 'CREATE_CONVERSATION_RESOURCE',
params: { userid: 'cafebabe' },
data: {
type: 'conversation',
attributes: { recipient: 'deadbeef' },
relationships: {
messages: {
data: [{
type: 'message',
method: 'create',
'temp-id': expect.any(String),
}],
},
},
},
included: [{
type: 'message',
'temp-id': expect.any(String),
attributes: {
text: 'Hello world !',
clientId: '0ff1ce',
},
}],
});
});
});
});
});
});

Loading