简体   繁体   中英

Use nuxeo client sdk for RESTClient by Angular 6

I want to use nuxeo ClientSdk to consume its REST api in my Angular 6 client, but i can't use it because this is a javascript package and it don't have any types definition for typescript.

However i tried to include this library in my project using this import statement in my custuom service:

import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable, of} from 'rxjs';
import { Http, Response , RequestOptions , Headers } from '@angular/http';
import { Data } from './model/genericData';
import * as NuxeoSdk from 'nuxeo';

const username = 'Administrator';
const password = 'Administrator';
const httpOptions = {
  headers : new HttpHeaders({
    'Authorization' : 'Basic ' + btoa(username + ':' + password),
    'X-NXDocumentProperties' : '*'
  })
};

/*const headers = new HttpHeaders();
headers.append('Authorization', 'Basic ' + btoa('Administrator:Administrator'));
headers.append('X-NXDocumentProperties', '*');*/
const baseAddr = 'http://dev2017-publicwebserver-alb-59603702.eu-west-1.elb.amazonaws.com/nuxeo/';
const serviceApiEndpoint = 'api/v1/';
const methodId = 'id/';
const childrenQuery = '/@children?';
const RootId = '1ca2e2f5-4e9e-4c98-afc6-95b467c359fc';

@Injectable({
  providedIn: 'root'
})

export class NuxeoService {
  constructor(private http: HttpClient) {}

  getJsonById(id: string): Observable<Data> {

    // this.http.get(this.baseAddr + this.methodId + id, httpOptions).subscribe(res => );
    return this.http.get<Data>(baseAddr + serviceApiEndpoint + methodId + id, httpOptions);
  }

  getRoot(): Observable<Data> {
    // this.http.get(this.baseAddr + this.methodId + id, httpOptions).subscribe(res => );
    return this.http.get<Data>(baseAddr + serviceApiEndpoint + methodId + RootId, httpOptions);
  }

  getDomains(): Observable<Data> {
    return this.http.get<Data>(baseAddr + serviceApiEndpoint + methodId + RootId + childrenQuery, httpOptions);
  }

  getChildrenById(id: string): Observable<Data> {
    return this.http.get<Data>(baseAddr + serviceApiEndpoint + methodId + id + childrenQuery, httpOptions);
  }

  getNuxeoResource(): void {
    const nuxeo = new NuxeoSdk({
      baseURL: baseAddr,
      auth: {
       method: 'basic',
       username: username,
        password: password
      }
    });

    nuxeo.request('path/')
      .get()
      .then(function (doc) {
        console.log('doc-> ', doc);
      });
  }
}

The problem is that, when i run this angular app this error occurred:

index.js:1 Uncaught ReferenceError: process is not defined
    at Object../node_modules/promise-queue/index.js (index.js:1)
    at __webpack_require__ (bootstrap:76)
    at Object../node_modules/nuxeo/lib/upload/batch.js (batch.js:5)
    at __webpack_require__ (bootstrap:76)
    at Object../node_modules/nuxeo/lib/operation.js (operation.js:7)
    at __webpack_require__ (bootstrap:76)
    at Object../node_modules/nuxeo/lib/nuxeo.js (nuxeo.js:4)
    at __webpack_require__ (bootstrap:76)
    at Object../node_modules/nuxeo/lib/index.js (index.js:1)
    at __webpack_require__ (bootstrap:76)
./node_modules/promise-queue/index.js   @   index.js:1
__webpack_require__ @   bootstrap:76
./node_modules/nuxeo/lib/upload/batch.js    @   batch.js:5
__webpack_require__ @   bootstrap:76
./node_modules/nuxeo/lib/operation.js   @   operation.js:7
__webpack_require__ @   bootstrap:76
./node_modules/nuxeo/lib/nuxeo.js   @   nuxeo.js:4
__webpack_require__ @   bootstrap:76
./node_modules/nuxeo/lib/index.js   @   index.js:1
__webpack_require__ @   bootstrap:76
./src/app/nuxeo.service.ts  @   DynamicFlatNode.ts:8
__webpack_require__ @   bootstrap:76
./src/app/app.module.ts @   app.component.ts:176
__webpack_require__ @   bootstrap:76
./src/main.ts   @   environment.ts:15
__webpack_require__ @   bootstrap:76
0   @   main.ts:12
__webpack_require__ @   bootstrap:76
checkDeferredModules    @   bootstrap:43
webpackJsonpCallback    @   bootstrap:30
(anonymous) @   main.js:1

imho i have been doing the import procedure wrong, so any idea to going this problem solve?

and the library i wanna import is :

const Nuxeo = require('./nuxeo');
const Base = require('./base');
const Operation = require('./operation');
const Request = require('./request');
const Repository = require('./repository');
const Document = require('./document');
const BatchUpload = require('./upload/batch');
const Blob = require('./blob');
const BatchBlob = require('./upload/blob');
const Users = require('./user/users');
const User = require('./user/user');
const Groups = require('./group/groups');
const Group = require('./group/group');
const Directory = require('./directory/directory');
const DirectoryEntry = require('./directory/entry');
const Workflows = require('./workflow/workflows');
const Workflow = require('./workflow/workflow');
const Task = require('./workflow/task');
const constants = require('./deps/constants');
const Promise = require('./deps/promise');
const {
  basicAuthenticator,
  tokenAuthenticator,
  bearerTokenAuthenticator,
  portalAuthenticator,
} = require('./auth/auth');
const {
  documentUnmarshaller,
  documentsUnmarshaller,
  workflowUnmarshaller,
  workflowsUnmarshaller,
  taskUnmarshaller,
  tasksUnmarshaller,
  directoryEntryUnmarshaller,
  directoryEntriesUnmarshaller,
  userUnmarshaller,
  groupUnmarshaller,
} = require('./unmarshallers/unmarshallers');
const NuxeoVersions = require('./nuxeo-versions');
const { SERVER_VERSIONS } = require('./server-version');
const oauth2 = require('./auth/oauth2');

const pkg = require('../package.json');

Nuxeo.Base = Base;
Nuxeo.Operation = Operation;
Nuxeo.Request = Request;
Nuxeo.Repository = Repository;
Nuxeo.Document = Document;
Nuxeo.BatchUpload = BatchUpload;
Nuxeo.Blob = Blob;
Nuxeo.BatchBlob = BatchBlob;
Nuxeo.Users = Users;
Nuxeo.User = User;
Nuxeo.Groups = Groups;
Nuxeo.Group = Group;
Nuxeo.Directory = Directory;
Nuxeo.DirectoryEntry = DirectoryEntry;
Nuxeo.Workflows = Workflows;
Nuxeo.Workflow = Workflow;
Nuxeo.Task = Task;
Nuxeo.constants = constants;
Nuxeo.version = pkg.version;

// expose Nuxeo versions
Nuxeo.VERSIONS = NuxeoVersions;
// expose Nuxeo Server versions
Nuxeo.SERVER_VERSIONS = SERVER_VERSIONS;

Nuxeo.oauth2 = oauth2;

Nuxeo.promiseLibrary(Promise);

// register default authenticators
Nuxeo.registerAuthenticator('basic', basicAuthenticator);
Nuxeo.registerAuthenticator('token', tokenAuthenticator);
Nuxeo.registerAuthenticator('bearerToken', bearerTokenAuthenticator);
Nuxeo.registerAuthenticator('portal', portalAuthenticator);

// register default unmarshallers
Nuxeo.registerUnmarshaller('document', documentUnmarshaller);
Nuxeo.registerUnmarshaller('documents', documentsUnmarshaller);
Nuxeo.registerUnmarshaller('workflow', workflowUnmarshaller);
Nuxeo.registerUnmarshaller('workflows', workflowsUnmarshaller);
Nuxeo.registerUnmarshaller('task', taskUnmarshaller);
Nuxeo.registerUnmarshaller('tasks', tasksUnmarshaller);
Nuxeo.registerUnmarshaller('directoryEntry', directoryEntryUnmarshaller);
Nuxeo.registerUnmarshaller('directoryEntries', directoryEntriesUnmarshaller);
Nuxeo.registerUnmarshaller('user', userUnmarshaller);
Nuxeo.registerUnmarshaller('group', groupUnmarshaller);
// make the WorkflowsUnmarshaller work for Nuxeo 7.10
Nuxeo.registerUnmarshaller('worflows', workflowsUnmarshaller);

module.exports = Nuxeo;

You can have a look at the service generated by nuxeo CLI when bootstraping an Angular project. In short, it adds the following line when instantiating the client :

  Object.getOwnPropertyNames(Nuxeo.prototype).forEach(name => {
      if (/^_|constructor/.test(name)) {
          return;
      }

      NuxeoService.prototype[name] = function (...args: any[]) {
          return nuxeo[name].apply(nuxeo, args);
      };
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM