Invoking a Custom Endpoint that you have created on your Kinvey backend requires different approaches on web and mobile.


Web


On a web view (called blank-web-view in this example), use the DefaultDataProvider to invoke an endpoint. It gives you access to various Kinvey backend features.


import { Inject, Injector } from '@angular/core';
import { BlankWebViewViewBaseComponent } from '@src/app/modules/mymod/blank-web-view/blank-web-view.base.component';
import { HttpClient } from '@angular/common/http';
import { DataProviderService } from '@src/app/core/data/data-provider.service';

export class BlankWebViewViewComponent extends BlankWebViewViewBaseComponent {
    private http: HttpClient;
    private dataProviderService: DataProviderService;

    constructor(@Inject(Injector) injector: Injector) {
        super(injector);

        this.http = injector.get(HttpClient);
        this.dataProviderService = injector.get(DataProviderService);
    }

    // Fired when component is initialized and input properties are set
    public onInit(): void {
        this.callCustomEndpoint('my-endpoint', {
            name: "Fred Jones",
            eyes: "Blue"
        }).then(response => {
            console.log(response);
        })
    }

    public callCustomEndpoint(endpoint: string, data: any) {
        const defaultDataProvider = 'DefaultDataProvider';
        const dataProvider = this.dataProviderService.get(defaultDataProvider);
        const interceptorAuthHeader = { 'x-kuib-data-provider': defaultDataProvider };

        const customEndpoint = `${dataProvider.serviceUri}/rpc/${dataProvider.appKey}/custom/${endpoint}`

        return this.http.request('POST', customEndpoint, {
            headers: interceptorAuthHeader,
            body: data
        }).toPromise();
    }
}


Mobile


On a mobile view (called blank-mobile-view in this example), use the execute() Kinvey SDK method from the CustomEndpoint namespace to invoke an endpoint.


import { Inject, Injector } from '@angular/core';
import { BlankMobileViewViewBaseComponent } from '@src/app/modules/mymod/blank-mobile-view/blank-mobile-view.base.component';
import { Kinvey } from 'kinvey-nativescript-sdk';

export class BlankMobileViewViewComponent extends BlankMobileViewViewBaseComponent {
    constructor(@Inject(Injector) injector: Injector) {
        super(injector);
    }

    ngOnInit() {
        Kinvey.CustomEndpoint.execute('my-endpoint', {
            name: "Fred Jones",
            eyes: "Blue"
        }).then(response => {
            console.log(response);
        });
    }
}