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 24, 2018
1 parent ec37f2f commit 0c7bfaa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
38 changes: 18 additions & 20 deletions docs/site/Model.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,9 @@ import {model, property} from '@loopback/repository';

@model()
export class Customer {
@property()
email: string;
@property()
isMember: boolean;
@property()
cart: ShoppingCart;
@property() email: string;
@property() isMember: boolean;
@property() cart: ShoppingCart;
}
```

Expand All @@ -112,11 +109,9 @@ export class Product extends Entity {
})
id: number;

@property()
name: string;
@property() name: string;

@property()
slug: string;
@property() slug: string;

constructor(data?: Partial<Product>) {
super(data);
Expand Down Expand Up @@ -147,6 +142,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 Expand Up @@ -218,8 +221,7 @@ to determine the type of a particular property.
```ts
@model()
class Product extends Entity {
@property()
public name: string; // The type information for this property is String.
@property() public name: string; // The type information for this property is String.
}
```

Expand All @@ -239,15 +241,13 @@ adds the appropriate metadata for type inference of your array properties.
```ts
@model()
class Order extends Entity {
@property.array(Product)
items: Product[];
@property.array(Product) items: Product[];
}

@model()
class Thread extends Entity {
// Note that we still require it, even for primitive types!
@property.array(String)
posts: string[];
@property.array(String) posts: string[];
}
```

Expand Down Expand Up @@ -280,16 +280,14 @@ import {getJsonSchema} from '@loopback/repository-json-schema';

@model()
class Category {
@property()
name: string;
@property() name: string;
}

@model()
class Product {
@property({required: true})
name: string;
@property()
type: Category;
@property() type: Category;
}

const jsonSchema = getJsonSchema(Product);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,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 () => {
const p = await repo.create({slug: 'pencil'});
p.name = 'Red Pencil';
await repo.save(p);
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',
} as AnyObject);

await expect(pencilRepo.deleteById(10000)).to.be.fulfilledWith(false);
});

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

0 comments on commit 0c7bfaa

Please sign in to comment.