1

Is it possible to use JSON Schema faker as a third party dependency in Angular. I tried to use the dependency injection to Angular however in the providers I am not able to import jsonSchemaFaker.

angular.json

"scripts": [
    "./node_modules/json-schema-faker/dist/json-schema-faker.bundle.min.js"
]

jsonSchemaFaker.service.ts

import { InjectionToken } from '@angular/core';
export const JSF_Token = new InjectionToken ('jsonSchemaFaker');

app.module.ts

providers: [
    { provide: JSF_Token, useValue: jsf }
]

...

declare let jsf: any;

This is what I tried to Inject json schema faker as a dependency in my angular app.. I am getting .. Uncaught ReferenceError: jsf is not defined

Raghavan
  • 61
  • 5
  • Please provide some potentially incomplete code of what you're trying to achieve so SO users will better understand your requirements – Nino Filiu Feb 26 '19 at 10:48
  • @NinoFiliu .. I've added the code (in my query) that I used to work with JSON Schema Faker in my angular application. – Raghavan Feb 26 '19 at 11:24

2 Answers2

1

That is not how you use npm packages in an angular application.

First off, navigate to the directory of your package.json in your application and install the package:

npm install json-schema-faker

Then, inside your components or your services, use it as such:

// at the top of your file, next to other imports
import jsf from 'json-schema-faker';

// json-schema-faker is now available in the rest of your file as jsf

// you can, for example, have a service method that returns that:
jsf.generate({type: 'string'})
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
  • I tried above steps however I am getting some warnings and errors. WARNING in ./node_modules/escodegen/node_modules/source-map/lib/source-map/source-map-generator.js 8:45-52 Critical dependency: require function is used in a way in which dependencies cannot be statically extracted ERROR in ./node_modules/jsonpath/generated/parser.js Module not found: Error: Can't resolve 'fs' in 'D:\projects\esb\Coupler\frontend\client\node_modules\jsonpath\generated' – Raghavan Feb 26 '19 at 14:17
  • I don't have enough infos to debug your issue. If you require further help, please put your code inside a clonable git repo – Nino Filiu Feb 26 '19 at 14:55
  • I have tried an stackblitz implementation. However I am unable to continue with the implementation. https://stackblitz.com/edit/angular-json-schema-faker-implement – Raghavan Feb 28 '19 at 05:43
0

I had to change my providers and the declaration to

providers: [
    { provide: JSF_Token, useValue: JSONSchemaFaker }
]

declare let JSONSchemaFaker: any;

Reason: the global name for JSON Schema Faker mentioned in that library is "JSONSchemaFaker". It was a mistake on my part to declare it as jsf.

Raghavan
  • 61
  • 5