Skip to content

Commit f68a45c

Browse files
Hage Yaapahacksparrow
authored andcommitted
feat: default 404 for request to non-existent resource
send 404 for requests to non-existent resoure by default.
1 parent a408377 commit f68a45c

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

docs/site/Model.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ class MyFlexibleModel extends Entity {
147147
}
148148
```
149149

150+
The default response for a delete request to a non-existent resource is a `404`.
151+
You can change this behavior to `200` by setting `strictDelete` to `false`.
152+
153+
```ts
154+
@model({settings: {strictDelete: false}})
155+
class Todo extends Entity { ... }
156+
```
157+
150158
### Model Decorator
151159

152160
The model decorator can be used without any additional parameters, or can be

packages/repository/src/repositories/legacy-juggler-bridge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class DefaultCrudRepository<T extends Entity, ID>
125125
this.modelClass = dataSource.createModel<juggler.PersistedModelClass>(
126126
definition.name,
127127
properties,
128-
Object.assign({strict: true}, definition.settings),
128+
Object.assign({strict: true, strictDelete: true}, definition.settings),
129129
);
130130
this.modelClass.attachTo(dataSource);
131131
}

packages/repository/test/acceptance/repository.acceptance.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,36 @@ describe('Repository in Thinking in LoopBack', () => {
7171
expect(stored).to.containDeep({extra: 'additional data'});
7272
});
7373

74+
it('enables strict delete by default', async () => {
75+
await repo.create({slug: 'pencil'});
76+
await expect(repo.deleteById(10000)).to.be.rejectedWith(
77+
/No instance with id/,
78+
);
79+
});
80+
81+
it('disables strict delete via configuration', async () => {
82+
@model({settings: {strictDelete: false}})
83+
class Pencil extends Entity {
84+
@property({id: true})
85+
id: number;
86+
@property({type: 'string'})
87+
name: string;
88+
}
89+
90+
const pencilRepo = new DefaultCrudRepository<
91+
Pencil,
92+
typeof Pencil.prototype.id
93+
>(Pencil, new DataSource({connector: 'memory'}));
94+
95+
await pencilRepo.create({
96+
name: 'Green Pencil',
97+
});
98+
99+
// When `strictDelete` is set to `false`, `deleteById()` on a non-existing
100+
// resource is resolved with `false`, instead of being rejected.
101+
await expect(pencilRepo.deleteById(10000)).to.be.fulfilledWith(false);
102+
});
103+
74104
function givenProductRepository() {
75105
const db = new DataSource({
76106
connector: 'memory',

0 commit comments

Comments
 (0)