buglabs / node-xml2json

Converts XML to JSON using node-expat
809 stars 210 forks source link

error TS2304: Cannot find name 'require'. #165

Open robeverett opened 6 years ago

robeverett commented 6 years ago

I'm trying to return a SOAP response in Angular 6.0 httpClient typescript, so I need to convert XML to JSON.

How do I used NodeRequire in typescript ?

I'm trying to do this: eturn this.http.post(environment.engageServiceUrl + '?op=RetrieveEmployees', soapPayload, httpOptions) .map(data => { var xmlJsn = require('node_modules/xml2js'); console.log(data); },

but it won't build.

I've tried to follow this example: https://stackoverflow.com/questions/36368405/how-to-parse-xml-in-angular-2

After running npm install.... and adding this in my service.ts: import * as xml2js from 'node_modules/xml2js';

It builds but I get this warning:

[ts] Could not find a declaration file for module 'node_modules/xml2js'... Try 'npm install @types/xml2js' if it exists or add a new declaration (d.ts) file containing 'declare module 'xml2js';`

I did that and still get the same.

xamgore commented 5 years ago

The package is written in pure javascript, so there are no type definitions of toJson & toXml functions. That's why the compiler raises an error.

You can define a declaration module yourself (depends on tsconfig.json). For example, put the following file into types/xml2json.d.ts:

declare module 'xml2json' {

  export type ToJsonOptions = {
    // Returns a Javascript object instead of a JSON string
    object: boolean;
    // Makes the JSON reversible to XML
    reversible: boolean;
    // Makes type coercion. i.e.: numbers and booleans present in attributes and
    // element values are converted from  string to its correspondent data types.
    // Coerce can be optionally defined as an object with specific methods of
    // coercion based on attribute name or tag name, with fallback to default coercion.
    coerce: boolean;
    // Sanitizes the following characters present in element values: < > ( ) # & " '
    sanitize: boolean;
    // Removes leading and trailing whitespaces as well as line terminators in element values.
    trim: boolean;
    // XML child nodes are always treated as arrays NB: you can specify
    // a selective array of nodes for this to apply to instead of the whole document.
    arrayNotation: boolean;
    // Changes the default textNode property from $t to _t when option is set to true.
    // Alternatively a string can be specified which will override $t to what ever the string is.
    alternateTextNode: boolean;
  }

  export type ToXmlOptions = {
    // Sanitizes the following characters present in element values: < > ( ) # & " '
    sanitize: boolean;
    // Ignores all null values
    ignoreNull: boolean;
  }

  export function toJson(xml: string, options?: Partial<ToJsonOptions>): string | object
  export function toXml(json: string, options?: Partial<ToXmlOptions>): string
}

Usage:

import xml2json from 'xml2json'

const json = xml2json.toJson(str) as string
const data = xml2json.toJson(str, { object: true }) as SomeYoursData