angular web memory

  • B4_644918
    了解作者
  • 130.1KB
    文件大小
  • 文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-01 03:42
    上传日期
angular web memory
angular-in-memory-web-api.rar
  • angular-in-memory-web-api
  • bundles
  • in-memory-web-api.umd.js
    205.5KB
  • interfaces.d.ts
    7.8KB
  • in-memory-web-api.module.metadata.json
    3.8KB
  • backend.service.metadata.json
    3.4KB
  • http-status-codes.d.ts
    10.6KB
  • http-status-codes.js.map
    28.7KB
  • http-in-memory-web-api.module.js
    2.5KB
  • interfaces.js.map
    12KB
  • http-client-in-memory-web-api.module.metadata.json
    3.1KB
  • http-client-backend.service.js.map
    6.1KB
  • http-backend.service.js.map
    7.2KB
  • index.metadata.json
    705B
  • http-status-codes.js
    20.8KB
  • http-client-in-memory-web-api.module.js
    2.6KB
  • backend.service.d.ts
    9.3KB
  • LICENSE
    1KB
  • http-backend.service.d.ts
    1.9KB
  • http-backend.service.metadata.json
    2.5KB
  • README.md
    18.3KB
  • index.d.ts
    338B
  • in-memory-web-api.module.d.ts
    1.3KB
  • http-in-memory-web-api.module.metadata.json
    3KB
  • index.js.map
    643B
  • http-client-in-memory-web-api.module.d.ts
    1.6KB
  • http-client-in-memory-web-api.module.js.map
    3.9KB
  • in-memory-web-api.module.js.map
    3.8KB
  • in-memory-web-api.module.js
    2.5KB
  • http-in-memory-web-api.module.js.map
    3.8KB
  • index.js
    371B
  • http-client-backend.service.metadata.json
    2.5KB
  • backend.service.js
    26KB
  • http-backend.service.js
    4.8KB
  • http-status-codes.metadata.json
    38.9KB
  • CHANGELOG.md
    15.1KB
  • http-in-memory-web-api.module.d.ts
    1.5KB
  • http-client-backend.service.d.ts
    1.9KB
  • interfaces.metadata.json
    2KB
  • backend.service.js.map
    43KB
  • http-client-backend.service.js
    4.2KB
  • package.json
    3.5KB
  • interfaces.js
    3KB
内容介绍
# Angular in-memory-web-api [![Build Status][travis-badge]][travis-badge-url] An in-memory web api for Angular demos and tests that emulates CRUD operations over a RESTy API. It intercepts Angular `Http` and `HttpClient` requests that would otherwise go to the remote server and redirects them to an in-memory data store that you control. --- ## Important recent changes ### HTTP response data no longer wrapped in object w/ `data` property As of v0.5.0 (5 October 2017), the `dataEncapsulation` configuration default changed from `true` to `false`. The HTTP response body holds the data values directly rather than an object that encapsulates those values, `{data: ...}`. This is a **breaking change that affects almost all existing apps!** See the [CHANGELOG](https://github.com/angular/in-memory-web-api/blob/master/CHANGELOG.md/#0.5.0) for the reason behind this change and how to quickly fix your code or revert to encapsulation. ### v0.4 supports `HttpClient` Release v0.4.0 (8 Sept 2017) was a major overhaul of this library. The v0.4.0 release introduced **breaking changes** that affect developers who used the customization features or loaded application files with SystemJS. **Read this README and the [CHANGELOG](https://github.com/angular/in-memory-web-api/blob/master/CHANGELOG.md/#0.4.0)** to learn what's new and about other **breaking changes**. --- ## Use cases * Demo apps that need to simulate CRUD data persistence operations without a real server. You won't have to build and start a test server. * Whip up prototypes and proofs of concept. * Share examples with the community in a web coding environment such as Plunker or CodePen. Create Angular issues and StackOverflow answers supported by live code. * Simulate operations against data collections that aren't yet implemented on your dev/test server. You can pass requests thru to the dev/test server for collections that are supported. * Write unit test apps that read and write data. Avoid the hassle of intercepting multiple http calls and manufacturing sequences of responses. The in-memory data store resets for each test so there is no cross-test data pollution. * End-to-end tests. If you can toggle the app into test mode using the in-memory web api, you won't disturb the real database. This can be especially useful for CI (continuous integration) builds. >**LIMITATIONS** > >The _in-memory-web-api_ exists primarily to support the [Angular documentation](https://angular.io/docs/ts/latest/ "Angular documentation web site"). It is not supposed to emulate every possible real world web API and is not intended for production use. > >Most importantly, it is ***always experimental***. We will make breaking changes and we won't feel bad about it because this is a development tool, not a production product. We do try to tell you about such changes in the `CHANGELOG.md` and we fix bugs as fast as we can. ## HTTP request handling This in-memory web api service processes an HTTP request and returns an `Observable` of HTTP `Response` object in the manner of a RESTy web api. It natively handles URI patterns in the form `:base/:collectionName/:id?` Examples: ``` // for requests to an `api` base URL that gets heroes from a 'heroes' collection GET api/heroes // all heroes GET api/heroes/42 // the hero with id=42 GET api/heroes?name=^j // 'j' is a regex; returns heroes whose name starting with 'j' or 'J' GET api/heroes.json/42 // ignores the ".json" ``` The in-memory web api service processes these requests against a "database" - a set of named collections - that you define during setup. ## Basic setup <a id="createDb" rel='nofollow' onclick='return false;'></a> Create an `InMemoryDataService` class that implements `InMemoryDbService`. At minimum it must implement `createDb` which creates a "database" hash whose keys are collection names and whose values are arrays of collection objects to return or update. For example: ```ts import { InMemoryDbService } from 'angular-in-memory-web-api'; export class InMemHeroService implements InMemoryDbService { createDb() { let heroes = [ { id: 1, name: 'Windstorm' }, { id: 2, name: 'Bombasto' }, { id: 3, name: 'Magneta' }, { id: 4, name: 'Tornado' } ]; return {heroes}; } } ``` **Notes** * The in-memory web api library _currently_ assumes that every collection has a primary key called `id`. * The `createDb` method can be synchronous or asynchronous. It would have to be asynchronous if you initialized your in-memory database service from a JSON file. Return the database _object_, an _observable_ of that object, or a _promise_ of that object. The tests include an example of all three. * The in-memory web api calls your `InMemoryDbService` data service class's `createDb` method on two occasions. 1. when it handles the _first_ HTTP request 1. when it receives a `resetdb` [command](#commands). In the command case, the service passes in a `RequestInfo` object, enabling the `createDb` logic to adjust its behavior per the client request. See the tests for examples. ### Import the in-memory web api module Register your data store service implementation with the `HttpClientInMemoryWebApiModule` in your root `AppModule.imports` calling the `forRoot` static method with this service class and an optional configuration object: ```ts import { HttpClientModule } from '@angular/common/http'; import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api'; import { InMemHeroService } from '../app/hero.service'; @NgModule({ imports: [ HttpClientModule, HttpClientInMemoryWebApiModule.forRoot(InMemHeroService), ... ], ... }) export class AppModule { ... } ``` **_Notes_** * Always import the `HttpClientInMemoryWebApiModule` _after_ the `HttpClientModule` to ensure that the in-memory backend provider supersedes the Angular version. * You can setup the in-memory web api within a lazy loaded feature module by calling the `.forFeature` method as you would `.forRoot`. ### Setup for the older Angular _Http_ module You can still use the in-memory web api with the older `Http` module. ```ts import { HttpModule } from '@angular/http'; import { HttpInMemoryWebApiModule } from 'angular-in-memory-web-api'; import { InMemHeroService } from '../app/hero.service'; @NgModule({ imports: [ HttpModule, HttpInMemoryWebApiModule.forRoot(InMemHeroService), ... ], ... }) export class AppModule { ... } ``` ### Setup for both Angular HTTP modules Perhaps you have a hybrid app with BOTH Angular modules because you're migrating to `HttpClient` from `Http`. Or perhaps you've used this library before and you don't have time at this moment to re-do your module setup. There's a combo-module (`InMemoryWebApiModule`) that prepares for both of them. It has the same syntax from pre-`v0.4.0` days and it should "_just work_" as long as you aren't using the [advanced features described below](#advanced-features). ```ts import { HttpModule } from '@angular/http'; import { HttpClientModule } from '@angular/common/http'; import { InMemoryWebApiModule } from 'angular-in-memory-web-api'; import { InMemHeroService } from '../app/hero.service'; @NgModule({ imports: [ HttpModule, HttpClientModule, InMemoryWebApiModule.forRoot(InMemHeroService), ... ], ... }) export class AppModule { ... } ``` # Examples The tests (`src/app/*.spec.ts` files) in the [github repository](https://github.com/angular/in-memory-web-api/tree/master/src/app) are a good place to learn how to setup and use this in-memory web api library. See also the example source code in the official Angular.io documentation such as the [HttpClient](https://angular.io/guide/http) guide and the [Tour of Heroes](https://angular.io/tutorial/toh-pt6). # Advanced Features Some features are not readily apparent in the basic usage described above. ## Configuration arguments The `InMemoryBackendConfigArgs` defines a set of options. Add them as the second `
评论