UNPKG

mongodb-stitch

Version:

[![Join the chat at https://gitter.im/mongodb/stitch](https://badges.gitter.im/mongodb/stitch.svg)](https://gitter.im/mongodb/stitch?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

349 lines 13.5 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { EJSON } from "bson"; import { Method, StitchAuthRequest, StitchServiceErrorCode, StitchServiceError } from "mongodb-stitch-core-sdk"; import { AppResponseCodec } from "./apps/AppsResources"; import { AuthProviderResponseCodec } from "./authProviders/AuthProvidersResources"; import { ProviderConfigCodec } from "./authProviders/ProviderConfigs"; import { FunctionCreatorCodec, FunctionResponseCodec } from "./functions/FunctionsResources"; import { RuleCreatorCodec, RuleResponseCodec } from "./services/rules/RulesResources"; import { ServiceConfigCodec } from "./services/ServiceConfigs"; import { ServiceResponseCodec } from "./services/ServicesResources"; import { ConfirmationEmailCodec } from "./userRegistrations/UserRegistrationsResources"; import { UserCreatorCodec, UserResponseCodec } from "./users/UsersResources"; function applyMixins(derivedCtor, baseCtors) { baseCtors.forEach(function (baseCtor) { Object.getOwnPropertyNames(baseCtor.prototype).forEach(function (name) { derivedCtor.prototype[name] = baseCtor.prototype[name]; }); }); } var BasicResource = (function () { function BasicResource(adminAuth, url) { this.adminAuth = adminAuth; this.url = url; } return BasicResource; }()); function checkEmpty(response) { if (response.body === undefined) { throw new StitchServiceError("unexpected empty response", StitchServiceErrorCode.Unknown); } } var Listable = (function (_super) { __extends(Listable, _super); function Listable() { return _super !== null && _super.apply(this, arguments) || this; } Listable.prototype.list = function () { var _this = this; var reqBuilder = new StitchAuthRequest.Builder(); reqBuilder.withMethod(Method.GET).withPath(this.url); return this.adminAuth .doAuthenticatedRequest(reqBuilder.build()) .then(function (response) { return EJSON.parse(response.body).map(function (val) { return _this.codec.decode(val); }); }); }; return Listable; }(BasicResource)); var Gettable = (function (_super) { __extends(Gettable, _super); function Gettable() { return _super !== null && _super.apply(this, arguments) || this; } Gettable.prototype.get = function () { var _this = this; var reqBuilder = new StitchAuthRequest.Builder(); reqBuilder.withMethod(Method.GET).withPath(this.url); return this.adminAuth .doAuthenticatedRequest(reqBuilder.build()) .then(function (response) { return _this.codec.decode(EJSON.parse(response.body)); }); }; return Gettable; }(BasicResource)); var Removable = (function (_super) { __extends(Removable, _super); function Removable() { return _super !== null && _super.apply(this, arguments) || this; } Removable.prototype.remove = function () { var reqBuilder = new StitchAuthRequest.Builder(); reqBuilder.withMethod(Method.DELETE).withPath(this.url); return this.adminAuth .doAuthenticatedRequest(reqBuilder.build()) .then(function () { return; }); }; return Removable; }(BasicResource)); var Creatable = (function (_super) { __extends(Creatable, _super); function Creatable() { return _super !== null && _super.apply(this, arguments) || this; } Creatable.prototype.create = function (data) { var _this = this; var reqBuilder = new StitchAuthRequest.Builder(); reqBuilder .withMethod(Method.POST) .withPath(this.url) .withBody(EJSON.stringify(this.creatorCodec.encode(data), { relaxed: false })); return this.adminAuth .doAuthenticatedRequest(reqBuilder.build()) .then(function (response) { return _this.codec.decode(EJSON.parse(response.body)); }); }; return Creatable; }(BasicResource)); var Updatable = (function (_super) { __extends(Updatable, _super); function Updatable() { return _super !== null && _super.apply(this, arguments) || this; } Updatable.prototype.update = function (data) { var _this = this; var reqBuilder = new StitchAuthRequest.Builder(); reqBuilder .withMethod(Method.PUT) .withPath(this.url) .withBody(EJSON.stringify(data, { relaxed: false })); return this.adminAuth .doAuthenticatedRequest(reqBuilder.build()) .then(function (response) { return _this.updatableCodec.decode(EJSON.parse(response.body)); }); }; return Updatable; }(BasicResource)); var Enablable = (function (_super) { __extends(Enablable, _super); function Enablable() { return _super !== null && _super.apply(this, arguments) || this; } Enablable.prototype.enable = function () { var reqBuilder = new StitchAuthRequest.Builder(); reqBuilder.withMethod(Method.PUT).withPath(this.url + "/enable"); return this.adminAuth .doAuthenticatedRequest(reqBuilder.build()) .then(function () { return; }); }; return Enablable; }(BasicResource)); var Disablable = (function (_super) { __extends(Disablable, _super); function Disablable() { return _super !== null && _super.apply(this, arguments) || this; } Disablable.prototype.disable = function () { var reqBuilder = new StitchAuthRequest.Builder(); reqBuilder.withMethod(Method.PUT).withPath(this.url + "/disable"); return this.adminAuth .doAuthenticatedRequest(reqBuilder.build()) .then(function () { return; }); }; return Disablable; }(BasicResource)); var AuthProvider = (function (_super) { __extends(AuthProvider, _super); function AuthProvider() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.codec = new AuthProviderResponseCodec(); _this.updatableCodec = new AuthProviderResponseCodec(); return _this; } return AuthProvider; }(BasicResource)); applyMixins(AuthProvider, [ Gettable, Updatable, Removable, Enablable, Disablable ]); var AuthProviders = (function (_super) { __extends(AuthProviders, _super); function AuthProviders() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.codec = new AuthProviderResponseCodec(); _this.creatorCodec = new ProviderConfigCodec(); return _this; } AuthProviders.prototype.authProvider = function (providerId) { return new AuthProvider(this.adminAuth, this.url + "/" + providerId); }; return AuthProviders; }(BasicResource)); applyMixins(AuthProviders, [Listable, Creatable]); var UserRegistrations = (function (_super) { __extends(UserRegistrations, _super); function UserRegistrations() { return _super !== null && _super.apply(this, arguments) || this; } UserRegistrations.prototype.sendConfirmation = function (email) { var reqBuilder = new StitchAuthRequest.Builder(); reqBuilder .withMethod(Method.POST) .withPath(this.url + "/by_email/" + email + "/send_confirm"); return this.adminAuth .doAuthenticatedRequest(reqBuilder.build()) .then(function (response) { return new ConfirmationEmailCodec().decode(JSON.parse(response.body)); }); }; return UserRegistrations; }(BasicResource)); var User = (function (_super) { __extends(User, _super); function User() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.codec = new UserResponseCodec(); return _this; } return User; }(BasicResource)); applyMixins(User, [Gettable, Removable]); var Users = (function (_super) { __extends(Users, _super); function Users() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.codec = new UserResponseCodec(); _this.creatorCodec = new UserCreatorCodec(); return _this; } Users.prototype.user = function (uid) { return new User(this.adminAuth, this.url + "/" + uid); }; return Users; }(BasicResource)); applyMixins(Users, [Listable, Creatable]); var Function = (function (_super) { __extends(Function, _super); function Function() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.codec = new FunctionResponseCodec(); _this.updatableCodec = new FunctionCreatorCodec(); return _this; } return Function; }(BasicResource)); applyMixins(Function, [Gettable, Updatable, Removable]); var Functions = (function (_super) { __extends(Functions, _super); function Functions() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.codec = new FunctionResponseCodec(); _this.creatorCodec = new FunctionCreatorCodec(); return _this; } Functions.prototype.function = function (fid) { return new Function(this.adminAuth, this.url + "/" + fid); }; return Functions; }(BasicResource)); applyMixins(Functions, [Creatable, Listable]); var Rule = (function (_super) { __extends(Rule, _super); function Rule() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.codec = new RuleResponseCodec(); return _this; } return Rule; }(BasicResource)); applyMixins(Rule, [Gettable, Removable]); var Rules = (function (_super) { __extends(Rules, _super); function Rules() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.creatorCodec = new RuleCreatorCodec(); _this.codec = new RuleResponseCodec(); return _this; } return Rules; }(BasicResource)); applyMixins(Rules, [Creatable, Listable]); var Service = (function (_super) { __extends(Service, _super); function Service() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.codec = new ServiceResponseCodec(); _this.rules = new Rules(_this.adminAuth, _this.url + "/rules"); return _this; } return Service; }(BasicResource)); applyMixins(Service, [Gettable, Removable]); var Services = (function (_super) { __extends(Services, _super); function Services() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.creatorCodec = new ServiceConfigCodec(); _this.codec = new ServiceResponseCodec(); return _this; } Services.prototype.service = function (id) { return new Service(this.adminAuth, this.url + "/" + id); }; return Services; }(BasicResource)); applyMixins(Services, [Listable, Creatable]); var App = (function (_super) { __extends(App, _super); function App() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.codec = new AppResponseCodec(); _this.authProviders = new AuthProviders(_this.adminAuth, _this.url + "/auth_providers"); _this.functions = new Functions(_this.adminAuth, _this.url + "/functions"); _this.services = new Services(_this.adminAuth, _this.url + "/services"); _this.users = new Users(_this.adminAuth, _this.url + "/users"); _this.userRegistrations = new UserRegistrations(_this.adminAuth, _this.url + "/user_registrations"); return _this; } return App; }(BasicResource)); applyMixins(App, [Gettable, Removable]); var Apps = (function (_super) { __extends(Apps, _super); function Apps() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.codec = new AppResponseCodec(); return _this; } Apps.prototype.create = function (name, defaults) { if (defaults === void 0) { defaults = false; } var encodedApp = { name: name }; var req = new StitchAuthRequest.Builder() .withMethod(Method.POST) .withPath(this.url + "?defaults=" + defaults) .withBody(JSON.stringify(encodedApp)) .build(); return this.adminAuth.doAuthenticatedRequest(req).then(function (response) { checkEmpty(response); return new AppResponseCodec().decode(EJSON.parse(response.body)); }); }; Apps.prototype.app = function (appId) { return new App(this.adminAuth, this.url + "/" + appId); }; return Apps; }(BasicResource)); applyMixins(Apps, [Listable]); export { Apps, App, BasicResource, Gettable, Updatable, Listable, Creatable, Enablable, Disablable, Removable, Services, Service, Rules, Rule, Functions, Function, Users, User, UserRegistrations, AuthProvider, AuthProviders, checkEmpty }; //# sourceMappingURL=Resources.js.map