Skip to content

Commit

Permalink
feat: default 404 for request to non-existent resource
Browse files Browse the repository at this point in the history
send 404 for requests to non-existent resoure by default.
  • Loading branch information
Hage Yaapa committed Aug 27, 2018
1 parent a408377 commit ddb8882
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/site/Model.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ class MyFlexibleModel extends Entity {
}
```

The default response for a delete request to a non-existent resource is a `404`.
You can change this behavior to `200` by setting `strictDelete` to `false`.

```ts
@model({settings: {strictDelete: false}})
class Todo extends Entity { ... }
```

### Model Decorator

The model decorator can be used without any additional parameters, or can be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class DefaultCrudRepository<T extends Entity, ID>
this.modelClass = dataSource.createModel<juggler.PersistedModelClass>(
definition.name,
properties,
Object.assign({strict: true}, definition.settings),
Object.assign({strict: true, strictDelete: true}, definition.settings),
);
this.modelClass.attachTo(dataSource);
}
Expand Down
30 changes: 30 additions & 0 deletions packages/repository/test/acceptance/repository.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ describe('Repository in Thinking in LoopBack', () => {
expect(stored).to.containDeep({extra: 'additional data'});
});

it('enables strict delete by default', async () => {
await repo.create({slug: 'pencil'});
await expect(repo.deleteById(10000)).to.be.rejectedWith(
/No instance with id/,
);
});

it('disables strict delete via configuration', async () => {
@model({settings: {strictDelete: false}})
class Pencil extends Entity {
@property({id: true})
id: number;
@property({type: 'string'})
name: string;
}

const pencilRepo = new DefaultCrudRepository<
Pencil,
typeof Pencil.prototype.id
>(Pencil, new DataSource({connector: 'memory'}));

await pencilRepo.create({
name: 'Green Pencil',
});

// When `strictDelete` is set to `false`, `deleteById()` on a non-existing
// resource is resolved with `false`, instead of being rejected.
await expect(pencilRepo.deleteById(10000)).to.be.fulfilledWith(false);
});

function givenProductRepository() {
const db = new DataSource({
connector: 'memory',
Expand Down

0 comments on commit ddb8882

Please sign in to comment.