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

doc: Please describe the difference between update and patch #147

Open
lorenzleutgeb opened this issue Jul 11, 2018 · 1 comment
Open

doc: Please describe the difference between update and patch #147

lorenzleutgeb opened this issue Jul 11, 2018 · 1 comment

Comments

@lorenzleutgeb
Copy link

Hello!

There are two methods update and patch and they look very similar. Could you please describe what is the difference between them and maybe add this to the documentation. Thank you!

@tomers
Copy link
Collaborator

tomers commented Jul 18, 2018

Indeed both methods are similar. Both eventually call PATCH request, but they have subtle differences, as update is called with modelName, while patch is called from a URL builder.

update usage:
jsonApi.update('foo', {title: 'foo'}, {include: 'something'})

patch usage:
jsonApi.one('foo', 1).patch({title: 'bar'})

Code:

  update (modelName, payload, params = {}, meta = {}) {
    let req = {
      method: 'PATCH',
      url: this.urlFor({model: modelName, id: payload.id}),
      model: modelName,
      data: payload,
      params: params,
      meta: meta
    }
    return this.runMiddleware(req)
  }

  patch (payload, params = {}, meta = {}) {
    let lastRequest = _last(this.builderStack)

    let req = {
      method: 'PATCH',
      url: this.urlFor(),
      model: _get(lastRequest, 'model'),
      data: payload,
      params,
      meta
    }

    if (this.resetBuilderOnCall) {
      this.resetBuilder()
    }

    return this.runMiddleware(req)
  }

Test code:

    it('should make basic update call', (done) => {
      let inspectorMiddleware = {
        name: 'inspector-middleware',
        req: (payload) => {
          expect(payload.req.method).to.be.eql('PATCH')
          expect(payload.req.url).to.be.eql('http://myapi.com/foos')
          expect(payload.req.data).to.be.eql({title: 'foo'})
          expect(payload.req.params).to.be.eql({include: 'something'})
          return {}
        }
      }

      jsonApi.middleware = [inspectorMiddleware]

      jsonApi.define('foo', {
        title: ''
      })

      jsonApi.update('foo', {title: 'foo'}, {include: 'something'})
        .then(() => done()).catch(() => done())
    })

it('should allow builders to be called with patch', (done) => {
      let inspectorMiddleware = {
        name: 'inspector-middleware',
        req: (payload) => {
          expect(payload.req.method).to.be.eql('PATCH')
          expect(payload.req.url).to.be.eql('http://myapi.com/foos/1')
          expect(payload.req.data).to.be.eql({title: 'bar'})
          return {}
        }
      }

      jsonApi.middleware = [inspectorMiddleware]

      jsonApi.one('foo', 1).patch({title: 'bar'}).then(() => done()).catch(() => done())
    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants