Installations
npm install @wheelerlaw/angular-in-memory-web-api
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
6.9.4
NPM Version
3.10.10
Score
58.7
Supply Chain
97.8
Quality
75.1
Maintenance
50
Vulnerability
97.6
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (87.25%)
JavaScript (12.75%)
Developer
angular
Download Statistics
Total Downloads
2,048
Last Day
1
Last Week
8
Last Month
9
Last Year
115
GitHub Statistics
1,181 Stars
112 Commits
230 Forks
43 Watching
6 Branches
25 Contributors
Package Meta Information
Latest Version
0.2.10
Package Id
@wheelerlaw/angular-in-memory-web-api@0.2.10
Size
57.51 kB
NPM Version
3.10.10
Node Version
6.9.4
Publised On
03 Feb 2017
Total Downloads
Cumulative downloads
Total Downloads
2,048
Last day
0%
1
Compared to previous day
Last week
0%
8
Compared to previous week
Last month
80%
9
Compared to previous month
Last year
-57.2%
115
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
3
Dev Dependencies
47
Angular in-memory-web-api
An in-memory web api for Angular demos and tests.
It intercepts Angular Http
requests that would otherwise go to the remote server
via the Angular XHRBackend
service
LIMITATIONS
The in-memory-web-api exists primarily to support the Angular documentation. 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.
UPDATE NOTICE
As of v.0.1.0, the npm package was renamed from
angular2-in-memory-web-api
to its current name,angular-in-memory-web-api
. All versions after 0.0.21 are shipped under this name. Be sure to update yourpackage.json
and import statements.
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 character 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"
Commands
The service also accepts "commands" that can, for example, reconfigure the service and reset the database.
When the last segment of the api base path is "commands", the collectionName
is treated as the command.
Example URLs:
commands/resetdb // Reset the "database" to its original state
commands/config // Get or update this service's config object
Commands are "hot", meaning they are always executed immediately whether or not someone subscribes to the returned observable.
Usage:
http.post('commands/resetdb', undefined);
http.get('commands/config');
http.post('commands/config', '{"delay":1000}');
Basic usage
Create an InMemoryDataService
class that implements InMemoryDataService
.
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:
1import { InMemoryDbService } from 'angular-in-memory-web-api'; 2 3export class InMemHeroService implements InMemoryDbService { 4 createDb() { 5 let heroes = [ 6 { id: '1', name: 'Windstorm' }, 7 { id: '2', name: 'Bombasto' }, 8 { id: '3', name: 'Magneta' }, 9 { id: '4', name: 'Tornado' } 10 ]; 11 return {heroes}; 12 } 13}
Register this module and your service implementation in AppModule.imports
calling the forRoot
static method with this service class and optional configuration object:
1// other imports 2import { HttpModule } from '@angular/http'; 3import { InMemoryWebApiModule } from 'angular-in-memory-web-api'; 4 5import { InMemHeroService } from '../app/hero-data'; 6@NgModule({ 7 imports: [ 8 HttpModule, 9 InMemoryWebApiModule.forRoot(InMemHeroService), 10 ... 11 ], 12 ... 13}) 14export class AppModule { ... }
See examples in the Angular.io such as the Server Communication and Tour of Heroes chapters.
Always import the
InMemoryWebApiModule
after theHttpModule
to ensure that theXHRBackend
provider of theInMemoryWebApiModule
supersedes all others.
Bonus Features
Some features are not readily apparent in the basic usage example.
The InMemoryBackendConfigArgs
defines a set of options. Add them as the second forRoot
argument:
1 InMemoryWebApiModule.forRoot(InMemHeroService, { delay: 500 }),
Read the InMemoryBackendConfigArgs
interface to learn about these options.
Request evaluation order
This service can evaluate requests in multiple ways depending upon the configuration. Here's how it reasons:
- If it looks like a command, process as a command
- If the HTTP method is overridden
- If the resource name (after the api base path) matches one of the configured collections, process that
- If not but the
Config.passThruUnknownUrl
flag istrue
, try to pass the request along to a real XHRBackend. - Return a 404.
See the handleRequest
method implementation for details.
Default delayed response
By default this service adds a 500ms delay (see InMemoryBackendConfig.delay
)
to all requests to simulate round-trip latency.
You can eliminate that or extend it by setting a different value:
1 InMemoryWebApiModule.forRoot(InMemHeroService, { delay: 0 }), // no delay
2 InMemoryWebApiModule.forRoot(InMemHeroService, { delay: 1500 }), // 1.5 second delay
Simple query strings
Pass custom filters as a regex pattern via query string. The query string defines which property and value to match.
Format: /app/heroes/?propertyName=regexPattern
The following example matches all names start with the letter 'j' or 'J' in the heroes collection.
/app/heroes/?name=^j
Search pattern matches are case insensitive by default. Set
config.caseSensitiveSearch = true
if needed.
Pass thru to a live XHRBackend
If an existing, running remote server should handle requests for collections
that are not in the in-memory database, set Config.passThruUnknownUrl: true
.
This service will forward unrecognized requests via a base version of the Angular XHRBackend
.
parseUrl and your override
The parseUrl
parses the request URL into a ParsedUrl
object.
ParsedUrl
is a public interface whose properties guide the in-memory web api
as it processes the request.
Default parseUrl
Default parsing depends upon certain values of config
: apiBase
, host
, and urlRoot
.
Read the source code for the complete story.
Configuring the apiBase
yields the most interesting changes to parseUrl
behavior:
-
For
apiBase=undefined
andurl='http://localhost/api/customers/42'
{base: 'api/', collectionName: 'customers', id: '42', ...}
-
For
apiBase='some/api/root/'
andurl='http://localhost/some/api/root/customers'
{base: 'some/api/root/', collectionName: 'customers', id: undefined, ...}
-
For
apiBase='/'
andurl='http://localhost/customers'
{base: '/', collectionName: 'customers', id: undefined, ...}
The actual api base segment values are ignored. Only the number of segments matters. The following api base strings are considered identical: 'a/b' ~ 'some/api/' ~ `two/segments'
This means that URLs that work with the in-memory web api may be rejected by the real server.
Custom parseUrl
You can override the default by implementing a parseUrl
method in your InMemoryDbService
.
Such a method must take the incoming request URL string and return a ParsedUrl
object.
Assign your alternative to InMemDbService['parseUrl']
responseInterceptor
You can morph the response returned by the default HTTP methods, called by collectionHandler
,
to suit your needs by adding a responseInterceptor
method to your InMemoryDbService
class.
The collectionHandler
calls your interceptor like this:
1responseOptions = this.responseInterceptor(responseOptions, requestInfo);
HTTP method interceptors
If you make requests this service can't handle but still want an in-memory database to hold values,
override the way this service handles any HTTP method by implementing a method in
your InMemoryDbService
that does the job.
The InMemoryDbService
method name must be the same as the HTTP method name but all lowercase.
This service calls it with an HttpMethodInterceptorArgs
object.
For example, your HTTP GET interceptor would be called like this:
e.g., yourInMemDbService["get"](interceptorArgs)
.
Your method must return an Observable<Response>
which should be "cold".
The HttpMethodInterceptorArgs
(as of this writing) are:
1requestInfo: RequestInfo; // parsed request 2db: Object; // the current in-mem database collections 3config: InMemoryBackendConfigArgs; // the current config 4passThruBackend: ConnectionBackend; // pass through backend, if it exists
Examples
The file examples/hero-data.service.ts
is an example of a Hero-oriented InMemoryDbService
,
derived from the HTTP Client
sample in the Angular documentation.
To try it, add the following line to AppModule.imports
1InMemoryWebApiModule.forRoot(HeroDataService)
That file also has a HeroDataOverrideService
derived class that demonstrates overriding
the parseUrl
method and it has a "cold" HTTP GET interceptor.
Add the following line to AppModule.imports
to see this version of the data service in action:
1InMemoryWebApiModule.forRoot(HeroDataOverrideService)
To Do
- add tests (shameful omission!)
Build Instructions
Mostly gulp driven.
The following describes steps for updating from one Angular version to the next
This is essential even when there are no changes of real consequence. Neglecting to synchronize Angular 2 versions triggers typescript definition duplication error messages when compiling your application project.
-
gulp bump
- up the package version number -
update
CHANGELOG.MD
to record the change -
update the dependent version(s) in
package.json
-
npm install
the new package(s) (make sure they really do install!)
npm list --depth=0
-
consider updating typings, install individually/several:
npm install @types/jasmine @types/node --save-dev
-
gulp clean
- clear out all generatedtext
-
npm run tsc
to confirm the project compiles w/o error (sanity check)
-- NO TESTS YET ... BAD --
-
gulp build
-
commit and push
-
npm publish
-
Fix and validate angular.io docs samples
-
Add two tags to the release commit with for unpkg
- the version number
- 'latest'
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/angular/.github/SECURITY.md:1
- Info: Found linked content: github.com/angular/.github/SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/angular/.github/SECURITY.md:1
- Info: Found text in security policy: github.com/angular/.github/SECURITY.md:1
Reason
Found 12/29 approved changesets -- score normalized to 4
Reason
branch protection is not maximal on development and all release branches
Details
- Info: 'allow deletion' disabled on branch 'master'
- Info: 'force pushes' disabled on branch 'master'
- Warn: 'branch protection settings apply to administrators' is disabled on branch 'master'
- Warn: branch 'master' does not require approvers
- Warn: codeowners review is not required on branch 'master'
- Warn: no status checks found to merge onto branch 'master'
- Warn: PRs are not required to make changes on branch 'master'; or we don't have data to detect it.If you think it might be the latter, make sure to run Scorecard with a PAT or use Repo Rules (that are always public) instead of Branch Protection settings
Reason
project is archived
Details
- Warn: Repository is archived.
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 17 are checked with a SAST tool
Reason
135 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-c75v-2vq8-878f
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-42xw-2xvc-qx8m
- Warn: Project is vulnerable to: GHSA-4w2v-q235-vp99
- Warn: Project is vulnerable to: GHSA-cph5-m8f7-6c5x
- Warn: Project is vulnerable to: GHSA-wf5p-g6vw-rhxx
- Warn: Project is vulnerable to: GHSA-pp7h-53gx-mx7r
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-c6rq-rjc2-86v2
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-897m-rjf5-jp39
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-9j49-mfvp-vmhm
- Warn: Project is vulnerable to: GHSA-pm9p-9926-w68m
- Warn: Project is vulnerable to: GHSA-9q64-mpxx-87fg
- Warn: Project is vulnerable to: GHSA-jc84-3g44-wf2q
- Warn: Project is vulnerable to: GHSA-vh7m-p724-62c2
- Warn: Project is vulnerable to: GHSA-r9p9-mrjm-926w
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-j4f2-536g-r55m
- Warn: Project is vulnerable to: GHSA-r7qp-cfhv-p84w
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-q42p-pg8m-cqh6
- Warn: Project is vulnerable to: GHSA-w457-6q6x-cgp9
- Warn: Project is vulnerable to: GHSA-62gr-4qp9-h98f
- Warn: Project is vulnerable to: GHSA-f52g-6jhx-586p
- Warn: Project is vulnerable to: GHSA-2cf5-4w76-r9qv
- Warn: Project is vulnerable to: GHSA-3cqr-58rm-57f8
- Warn: Project is vulnerable to: GHSA-g9r4-xpmj-mj65
- Warn: Project is vulnerable to: GHSA-q2c6-c6pm-g3gh
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-44pw-h2cw-w3vq
- Warn: Project is vulnerable to: GHSA-jp4x-w63m-7wgm
- Warn: Project is vulnerable to: GHSA-c429-5p7v-vgjp
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-6x33-pw7p-hmpq
- Warn: Project is vulnerable to: GHSA-pc5p-h8pf-mvwp
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-2pr6-76vf-7546
- Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-282f-qqgm-c34q
- Warn: Project is vulnerable to: GHSA-7x7c-qm48-pq9c
- Warn: Project is vulnerable to: GHSA-rc3x-jf5g-xvc5
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-89w7-5q45-r53w
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-82v2-mx6x-wq7q
- Warn: Project is vulnerable to: GHSA-4xcv-9jjx-gfj3
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-fhjf-83wg-r2j9
- Warn: Project is vulnerable to: GHSA-8hfj-j24r-96c4
- Warn: Project is vulnerable to: GHSA-wc69-rhjr-hc9g
- Warn: Project is vulnerable to: GHSA-4c7m-wxvm-r7gc / GHSA-pch5-whg9-qr2r
- Warn: Project is vulnerable to: GHSA-48ww-j4fc-435p
- Warn: Project is vulnerable to: GHSA-hwqf-gcqm-7353
- Warn: Project is vulnerable to: GHSA-9h6g-pr28-7cqp
- Warn: Project is vulnerable to: GHSA-cwx2-736x-mf6w
- Warn: Project is vulnerable to: GHSA-v39p-96qg-c8rf
- Warn: Project is vulnerable to: GHSA-8v63-cqqc-6r2c
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-gqgv-6jq5-jjj9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-35q2-47q7-3pc3
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-hjp8-2cm3-cc45
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
- Warn: Project is vulnerable to: GHSA-fxwf-4rqh-v8g3
- Warn: Project is vulnerable to: GHSA-25hc-qcg6-38wj
- Warn: Project is vulnerable to: GHSA-xfhh-g9f5-x4m4
- Warn: Project is vulnerable to: GHSA-qm95-pgcg-qqfq
- Warn: Project is vulnerable to: GHSA-cqmj-92xf-r6r9
- Warn: Project is vulnerable to: GHSA-j44m-qm6p-hp7m
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-f523-2f5j-gfcg
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-884p-74jh-xrg2
- Warn: Project is vulnerable to: GHSA-j7fq-p9q7-5wfv
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-xc7v-wxcw-j472
- Warn: Project is vulnerable to: GHSA-662x-fhqg-9p8v
- Warn: Project is vulnerable to: GHSA-394c-5j6w-4xmx
- Warn: Project is vulnerable to: GHSA-78cj-fxph-m83p
- Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3
- Warn: Project is vulnerable to: GHSA-cf4h-3jhx-xvhq
- Warn: Project is vulnerable to: GHSA-mgfv-m47x-4wqp
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-72mh-269x-7mh5
- Warn: Project is vulnerable to: GHSA-h4j5-c7cj-74xg
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
3.5
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More