ranna-ts 1.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/release.yml +32 -0
- package/.prettierrc.yml +6 -0
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/dist/client.d.ts +42 -0
- package/dist/client.js +65 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.js +35 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +15 -0
- package/dist/models.d.ts +70 -0
- package/dist/models.js +2 -0
- package/dist/request.d.ts +17 -0
- package/dist/request.js +60 -0
- package/dist/snippets.d.ts +99 -0
- package/dist/snippets.js +163 -0
- package/dist/ws/client.d.ts +52 -0
- package/dist/ws/client.js +75 -0
- package/dist/ws/models.d.ts +52 -0
- package/dist/ws/models.js +18 -0
- package/package.json +15 -0
- package/src/client.ts +76 -0
- package/src/errors.ts +45 -0
- package/src/index.ts +25 -0
- package/src/models.ts +79 -0
- package/src/request.ts +64 -0
- package/src/snippets.ts +155 -0
- package/src/ws/client.ts +97 -0
- package/src/ws/models.ts +63 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
name: Release CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- '*'
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
publish-gpr:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v2
|
|
13
|
+
- uses: actions/setup-node@v1
|
|
14
|
+
with:
|
|
15
|
+
node-version: 12
|
|
16
|
+
registry-url: https://npm.pkg.github.com/
|
|
17
|
+
- run: npx gitv
|
|
18
|
+
- run: npm run build
|
|
19
|
+
- run: npm publish
|
|
20
|
+
env:
|
|
21
|
+
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
|
22
|
+
|
|
23
|
+
publish-npm:
|
|
24
|
+
runs-on: ubuntu-latest
|
|
25
|
+
steps:
|
|
26
|
+
- uses: actions/checkout@v2
|
|
27
|
+
- uses: actions/setup-node@v1
|
|
28
|
+
- run: npx gitv
|
|
29
|
+
- run: npm run build
|
|
30
|
+
- run: npm publish
|
|
31
|
+
env:
|
|
32
|
+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
package/.prettierrc.yml
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 ranna
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ExecutionRequest, ExecutionResponse, SpecMap, SystemInfo } from './models';
|
|
2
|
+
import { ClientOptions, RequestClient } from './request';
|
|
3
|
+
import { WebSocketClient } from './ws/client';
|
|
4
|
+
/**
|
|
5
|
+
* Request client to perform API requests with.
|
|
6
|
+
*/
|
|
7
|
+
export declare class Client extends RequestClient {
|
|
8
|
+
/**
|
|
9
|
+
* Initialize a new client connected to the passed ranna
|
|
10
|
+
* server endpoint.
|
|
11
|
+
*
|
|
12
|
+
* You can specify additional options like the API version
|
|
13
|
+
* (default 'v1'), the user agent header or the authorization
|
|
14
|
+
* header, if required.
|
|
15
|
+
*
|
|
16
|
+
* @param endpoint The ranna server endpoint.
|
|
17
|
+
* @param options Additional options.
|
|
18
|
+
*/
|
|
19
|
+
constructor(endpoint: string, options?: ClientOptions);
|
|
20
|
+
/**
|
|
21
|
+
* Request the available spec map.
|
|
22
|
+
* @returns SpecMap
|
|
23
|
+
*/
|
|
24
|
+
spec(): Promise<SpecMap>;
|
|
25
|
+
/**
|
|
26
|
+
* Execute code.
|
|
27
|
+
* @param req Execution request
|
|
28
|
+
* @returns Execution response
|
|
29
|
+
*/
|
|
30
|
+
exec(req: ExecutionRequest, bypassCache?: boolean): Promise<ExecutionResponse>;
|
|
31
|
+
/**
|
|
32
|
+
* Request service information.
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
info(): Promise<SystemInfo>;
|
|
36
|
+
/**
|
|
37
|
+
* Connects to the ranna WebSocket API using
|
|
38
|
+
* the specified endpoint.
|
|
39
|
+
* @returns the websocket client.
|
|
40
|
+
*/
|
|
41
|
+
connectWs(): WebSocketClient;
|
|
42
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Client = void 0;
|
|
4
|
+
const request_1 = require("./request");
|
|
5
|
+
const client_1 = require("./ws/client");
|
|
6
|
+
const defaultOptions = {
|
|
7
|
+
version: 'v1',
|
|
8
|
+
userAgent: 'ranna-ts',
|
|
9
|
+
auth: null,
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Request client to perform API requests with.
|
|
13
|
+
*/
|
|
14
|
+
class Client extends request_1.RequestClient {
|
|
15
|
+
/**
|
|
16
|
+
* Initialize a new client connected to the passed ranna
|
|
17
|
+
* server endpoint.
|
|
18
|
+
*
|
|
19
|
+
* You can specify additional options like the API version
|
|
20
|
+
* (default 'v1'), the user agent header or the authorization
|
|
21
|
+
* header, if required.
|
|
22
|
+
*
|
|
23
|
+
* @param endpoint The ranna server endpoint.
|
|
24
|
+
* @param options Additional options.
|
|
25
|
+
*/
|
|
26
|
+
constructor(endpoint, options = defaultOptions) {
|
|
27
|
+
super(endpoint, options);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Request the available spec map.
|
|
31
|
+
* @returns SpecMap
|
|
32
|
+
*/
|
|
33
|
+
spec() {
|
|
34
|
+
return this.request('GET', 'spec');
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Execute code.
|
|
38
|
+
* @param req Execution request
|
|
39
|
+
* @returns Execution response
|
|
40
|
+
*/
|
|
41
|
+
exec(req, bypassCache = false) {
|
|
42
|
+
return this.request('POST', `exec?bypass_cache=${bypassCache}`, req);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Request service information.
|
|
46
|
+
* @returns
|
|
47
|
+
*/
|
|
48
|
+
info() {
|
|
49
|
+
return this.request('GET', 'info');
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Connects to the ranna WebSocket API using
|
|
53
|
+
* the specified endpoint.
|
|
54
|
+
* @returns the websocket client.
|
|
55
|
+
*/
|
|
56
|
+
connectWs() {
|
|
57
|
+
let endpoint = this.clientEndpoint;
|
|
58
|
+
if (endpoint.startsWith('https://'))
|
|
59
|
+
endpoint = 'wss://' + endpoint.substring(8);
|
|
60
|
+
else if (endpoint.startsWith('http://'))
|
|
61
|
+
endpoint = 'ws://' + endpoint.substring(7);
|
|
62
|
+
return new client_1.WebSocketClient(`${endpoint}/${this.clientOptions.version}/ws`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.Client = Client;
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ErrorResponse } from './models';
|
|
2
|
+
export declare class APIError extends Error implements ErrorResponse {
|
|
3
|
+
private _code;
|
|
4
|
+
private _status;
|
|
5
|
+
private _error;
|
|
6
|
+
private _context;
|
|
7
|
+
constructor(res: ErrorResponse);
|
|
8
|
+
get code(): number;
|
|
9
|
+
get status(): number;
|
|
10
|
+
get error(): string;
|
|
11
|
+
get context(): string;
|
|
12
|
+
}
|
|
13
|
+
export declare class ResponseError extends Error {
|
|
14
|
+
private _response;
|
|
15
|
+
constructor(res: Response);
|
|
16
|
+
get response(): Response;
|
|
17
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ResponseError = exports.APIError = void 0;
|
|
4
|
+
class APIError extends Error {
|
|
5
|
+
constructor(res) {
|
|
6
|
+
super(res.error);
|
|
7
|
+
this._code = res.code;
|
|
8
|
+
this._status = res.status;
|
|
9
|
+
this._error = res.error;
|
|
10
|
+
this._context = res.context;
|
|
11
|
+
}
|
|
12
|
+
get code() {
|
|
13
|
+
return this._code;
|
|
14
|
+
}
|
|
15
|
+
get status() {
|
|
16
|
+
return this._status;
|
|
17
|
+
}
|
|
18
|
+
get error() {
|
|
19
|
+
return this._error;
|
|
20
|
+
}
|
|
21
|
+
get context() {
|
|
22
|
+
return this._context;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.APIError = APIError;
|
|
26
|
+
class ResponseError extends Error {
|
|
27
|
+
constructor(res) {
|
|
28
|
+
super(`request failed: ${res.status}`);
|
|
29
|
+
this._response = res;
|
|
30
|
+
}
|
|
31
|
+
get response() {
|
|
32
|
+
return this._response;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.ResponseError = ResponseError;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { Client } from './client';
|
|
2
|
+
export { APIError, ResponseError } from './errors';
|
|
3
|
+
export { ExecutionRequest, ExecutionResponse, SpecMap, Spec, StringMap, Snippet, ErrorResponse, } from './models';
|
|
4
|
+
export { SnippetsClient } from './snippets';
|
|
5
|
+
export { ClientOptions } from './request';
|
|
6
|
+
export { WebSocketClient } from './ws/client';
|
|
7
|
+
export { Event, EventCode, LogData, Operation, StopData, Nonced, OpCode, RunID, WsError, } from './ws/models';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpCode = exports.EventCode = exports.WebSocketClient = exports.SnippetsClient = exports.ResponseError = exports.APIError = exports.Client = void 0;
|
|
4
|
+
var client_1 = require("./client");
|
|
5
|
+
Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return client_1.Client; } });
|
|
6
|
+
var errors_1 = require("./errors");
|
|
7
|
+
Object.defineProperty(exports, "APIError", { enumerable: true, get: function () { return errors_1.APIError; } });
|
|
8
|
+
Object.defineProperty(exports, "ResponseError", { enumerable: true, get: function () { return errors_1.ResponseError; } });
|
|
9
|
+
var snippets_1 = require("./snippets");
|
|
10
|
+
Object.defineProperty(exports, "SnippetsClient", { enumerable: true, get: function () { return snippets_1.SnippetsClient; } });
|
|
11
|
+
var client_2 = require("./ws/client");
|
|
12
|
+
Object.defineProperty(exports, "WebSocketClient", { enumerable: true, get: function () { return client_2.WebSocketClient; } });
|
|
13
|
+
var models_1 = require("./ws/models");
|
|
14
|
+
Object.defineProperty(exports, "EventCode", { enumerable: true, get: function () { return models_1.EventCode; } });
|
|
15
|
+
Object.defineProperty(exports, "OpCode", { enumerable: true, get: function () { return models_1.OpCode; } });
|
package/dist/models.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export interface Inline {
|
|
2
|
+
import_regex: string;
|
|
3
|
+
template: string;
|
|
4
|
+
}
|
|
5
|
+
export interface Spec {
|
|
6
|
+
image: string;
|
|
7
|
+
entrypoint: string;
|
|
8
|
+
cmd: string;
|
|
9
|
+
filename: string;
|
|
10
|
+
use: string;
|
|
11
|
+
registry: string;
|
|
12
|
+
language: string;
|
|
13
|
+
example: string;
|
|
14
|
+
inline: Inline;
|
|
15
|
+
}
|
|
16
|
+
export type SpecMap = {
|
|
17
|
+
[key: string]: Spec;
|
|
18
|
+
};
|
|
19
|
+
export type StringMap = {
|
|
20
|
+
[key: string]: string;
|
|
21
|
+
};
|
|
22
|
+
export interface ExecutionRequest {
|
|
23
|
+
language: string;
|
|
24
|
+
code: string;
|
|
25
|
+
arguments?: string[];
|
|
26
|
+
environment?: StringMap;
|
|
27
|
+
inline_expression: boolean;
|
|
28
|
+
}
|
|
29
|
+
export interface ExecutionResponse {
|
|
30
|
+
stdout: string;
|
|
31
|
+
stderr: string;
|
|
32
|
+
exectimems: number;
|
|
33
|
+
from_cache: boolean;
|
|
34
|
+
cache_date: string;
|
|
35
|
+
}
|
|
36
|
+
export interface ErrorResponse {
|
|
37
|
+
error: string;
|
|
38
|
+
code: number;
|
|
39
|
+
status: number;
|
|
40
|
+
context: string;
|
|
41
|
+
}
|
|
42
|
+
export interface Entity {
|
|
43
|
+
id: string;
|
|
44
|
+
timestamp: string;
|
|
45
|
+
}
|
|
46
|
+
export interface Snippet extends Entity {
|
|
47
|
+
ident: string;
|
|
48
|
+
displayname: string;
|
|
49
|
+
language: string;
|
|
50
|
+
code: string;
|
|
51
|
+
}
|
|
52
|
+
export interface SandboxInfo {
|
|
53
|
+
type: string;
|
|
54
|
+
version: string;
|
|
55
|
+
}
|
|
56
|
+
export interface SystemInfo {
|
|
57
|
+
sandbox: SandboxInfo;
|
|
58
|
+
version: string;
|
|
59
|
+
builddate: string;
|
|
60
|
+
go_version: string;
|
|
61
|
+
}
|
|
62
|
+
export interface User extends Entity {
|
|
63
|
+
username: string;
|
|
64
|
+
}
|
|
65
|
+
export interface UserCreate extends User {
|
|
66
|
+
masterkey: string;
|
|
67
|
+
}
|
|
68
|
+
export interface Token {
|
|
69
|
+
token: string;
|
|
70
|
+
}
|
package/dist/models.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrap additional options for the Client.
|
|
3
|
+
*/
|
|
4
|
+
export interface ClientOptions {
|
|
5
|
+
version: string;
|
|
6
|
+
userAgent: string | null;
|
|
7
|
+
auth: string | null;
|
|
8
|
+
}
|
|
9
|
+
export declare class RequestClient {
|
|
10
|
+
private endpoint;
|
|
11
|
+
private options;
|
|
12
|
+
constructor(endpoint: string, options: ClientOptions);
|
|
13
|
+
request<T>(method: 'GET' | 'POST' | 'PUT' | 'DELETE', path: string, body?: any): Promise<T>;
|
|
14
|
+
get clientOptions(): ClientOptions;
|
|
15
|
+
get clientEndpoint(): string;
|
|
16
|
+
set clientEndpoint(v: string);
|
|
17
|
+
}
|
package/dist/request.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.RequestClient = void 0;
|
|
13
|
+
const errors_1 = require("./errors");
|
|
14
|
+
class RequestClient {
|
|
15
|
+
constructor(endpoint, options) {
|
|
16
|
+
this.endpoint = endpoint;
|
|
17
|
+
this.options = options;
|
|
18
|
+
}
|
|
19
|
+
request(method, path, body) {
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
const headers = new Headers();
|
|
22
|
+
headers.set('Content-Type', 'application/json');
|
|
23
|
+
headers.set('Accept', 'application/json');
|
|
24
|
+
if (this.options.auth)
|
|
25
|
+
headers.set('Authorization', this.options.auth);
|
|
26
|
+
if (this.options.userAgent)
|
|
27
|
+
headers.set('User-Agent', this.options.userAgent);
|
|
28
|
+
const res = yield window.fetch(`${this.endpoint}/${this.options.version}/${path}`, {
|
|
29
|
+
method,
|
|
30
|
+
headers,
|
|
31
|
+
body: body ? JSON.stringify(body) : null,
|
|
32
|
+
credentials: this.options.auth ? 'include' : 'omit',
|
|
33
|
+
});
|
|
34
|
+
if (res.status === 204) {
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
if (!res.ok) {
|
|
38
|
+
let data;
|
|
39
|
+
try {
|
|
40
|
+
data = yield res.json();
|
|
41
|
+
}
|
|
42
|
+
catch (_a) {
|
|
43
|
+
throw new errors_1.ResponseError(res);
|
|
44
|
+
}
|
|
45
|
+
throw new errors_1.APIError(data);
|
|
46
|
+
}
|
|
47
|
+
return res.json();
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
get clientOptions() {
|
|
51
|
+
return this.options;
|
|
52
|
+
}
|
|
53
|
+
get clientEndpoint() {
|
|
54
|
+
return this.endpoint;
|
|
55
|
+
}
|
|
56
|
+
set clientEndpoint(v) {
|
|
57
|
+
this.endpoint = v;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.RequestClient = RequestClient;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { Snippet, Token, UserCreate } from './models';
|
|
2
|
+
import { ClientOptions, RequestClient } from './request';
|
|
3
|
+
/**
|
|
4
|
+
* Request client to perform API requests
|
|
5
|
+
* against the snippet API.
|
|
6
|
+
*/
|
|
7
|
+
export declare class SnippetsClient extends RequestClient {
|
|
8
|
+
/**
|
|
9
|
+
* Initialize a new client connected to the passed ranna
|
|
10
|
+
* server endpoint.
|
|
11
|
+
*
|
|
12
|
+
* You can specify additional options like the API version
|
|
13
|
+
* (default 'v1'), the user agent header or the authorization
|
|
14
|
+
* header, if required.
|
|
15
|
+
*
|
|
16
|
+
* @param endpoint The ranna snippets server endpoint.
|
|
17
|
+
* @param options Additional options.
|
|
18
|
+
*/
|
|
19
|
+
constructor(endpoint: string, options?: ClientOptions);
|
|
20
|
+
/**
|
|
21
|
+
* Creaates an account with the given username and returns
|
|
22
|
+
* the account properties including the account master key.
|
|
23
|
+
*
|
|
24
|
+
* This masterkey is exchanged againbst an API key later,
|
|
25
|
+
* which can be reviewed and regenerated. Also, the master
|
|
26
|
+
* key is required to delete an account.
|
|
27
|
+
*
|
|
28
|
+
* The masterkey is only shown ONCE at account creation
|
|
29
|
+
* and CAN NOT BE REVIEWED AGAIN AFTER THAT!
|
|
30
|
+
*
|
|
31
|
+
* @param username
|
|
32
|
+
* @returns
|
|
33
|
+
*/
|
|
34
|
+
createAccount(username: string): Promise<UserCreate>;
|
|
35
|
+
/**
|
|
36
|
+
* Exchanges the passed master key for the passed username
|
|
37
|
+
* against an API key which is used for subsequent requests.
|
|
38
|
+
*
|
|
39
|
+
* The API key can also be regenerated using this endpoint
|
|
40
|
+
* if already existent.
|
|
41
|
+
*
|
|
42
|
+
* You can set `store` to true to stroe the received API
|
|
43
|
+
* key in the request client.
|
|
44
|
+
*
|
|
45
|
+
* @param username
|
|
46
|
+
* @param masterkey
|
|
47
|
+
* @param store
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
exchangeAPIToken(username: string, masterkey: string, store?: boolean): Promise<Token>;
|
|
51
|
+
/**
|
|
52
|
+
* Deletes the user wtih the passed username if the passed
|
|
53
|
+
* masterkey matches the account.
|
|
54
|
+
*
|
|
55
|
+
* @param username
|
|
56
|
+
* @param masterkey
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
deleteAccount(username: string, masterkey: string): Promise<Token>;
|
|
60
|
+
/**
|
|
61
|
+
* Returns a list of snippets belonging to the
|
|
62
|
+
* authorized account.
|
|
63
|
+
*
|
|
64
|
+
* This endpoijt must be authorized with an API
|
|
65
|
+
* token.
|
|
66
|
+
*
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
list(): Promise<Snippet[]>;
|
|
70
|
+
/**
|
|
71
|
+
* Reqeusts a sinlie snippet by ident or id.
|
|
72
|
+
* @param ident
|
|
73
|
+
* @returns
|
|
74
|
+
*/
|
|
75
|
+
get(ident: string): Promise<Snippet>;
|
|
76
|
+
/**
|
|
77
|
+
* Creates a new snippet with the passed content.
|
|
78
|
+
* @param snippet
|
|
79
|
+
* @returns
|
|
80
|
+
*/
|
|
81
|
+
create(snippet: Snippet): Promise<Snippet>;
|
|
82
|
+
/**
|
|
83
|
+
* Updates a snippet with the passed content.
|
|
84
|
+
* @param snippet
|
|
85
|
+
* @returns
|
|
86
|
+
*/
|
|
87
|
+
update(snippet: Snippet): Promise<Snippet>;
|
|
88
|
+
/**
|
|
89
|
+
* Deletes a snipped by its ident or ID.
|
|
90
|
+
*
|
|
91
|
+
* This endpoint required authorization by the owner of
|
|
92
|
+
* the snippet. If the snippet was created anonymously,
|
|
93
|
+
* it can not be deleted!
|
|
94
|
+
*
|
|
95
|
+
* @param ident
|
|
96
|
+
* @returns
|
|
97
|
+
*/
|
|
98
|
+
delete(ident: string): Promise<{}>;
|
|
99
|
+
}
|
package/dist/snippets.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.SnippetsClient = void 0;
|
|
13
|
+
const request_1 = require("./request");
|
|
14
|
+
const defaultOptions = {
|
|
15
|
+
version: 'v1',
|
|
16
|
+
userAgent: 'ranna-ts',
|
|
17
|
+
auth: null,
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Request client to perform API requests
|
|
21
|
+
* against the snippet API.
|
|
22
|
+
*/
|
|
23
|
+
class SnippetsClient extends request_1.RequestClient {
|
|
24
|
+
/**
|
|
25
|
+
* Initialize a new client connected to the passed ranna
|
|
26
|
+
* server endpoint.
|
|
27
|
+
*
|
|
28
|
+
* You can specify additional options like the API version
|
|
29
|
+
* (default 'v1'), the user agent header or the authorization
|
|
30
|
+
* header, if required.
|
|
31
|
+
*
|
|
32
|
+
* @param endpoint The ranna snippets server endpoint.
|
|
33
|
+
* @param options Additional options.
|
|
34
|
+
*/
|
|
35
|
+
constructor(endpoint, options = defaultOptions) {
|
|
36
|
+
super(endpoint, options);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Creaates an account with the given username and returns
|
|
40
|
+
* the account properties including the account master key.
|
|
41
|
+
*
|
|
42
|
+
* This masterkey is exchanged againbst an API key later,
|
|
43
|
+
* which can be reviewed and regenerated. Also, the master
|
|
44
|
+
* key is required to delete an account.
|
|
45
|
+
*
|
|
46
|
+
* The masterkey is only shown ONCE at account creation
|
|
47
|
+
* and CAN NOT BE REVIEWED AGAIN AFTER THAT!
|
|
48
|
+
*
|
|
49
|
+
* @param username
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
createAccount(username) {
|
|
53
|
+
return this.request('PUT', `users/${username}`);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Exchanges the passed master key for the passed username
|
|
57
|
+
* against an API key which is used for subsequent requests.
|
|
58
|
+
*
|
|
59
|
+
* The API key can also be regenerated using this endpoint
|
|
60
|
+
* if already existent.
|
|
61
|
+
*
|
|
62
|
+
* You can set `store` to true to stroe the received API
|
|
63
|
+
* key in the request client.
|
|
64
|
+
*
|
|
65
|
+
* @param username
|
|
66
|
+
* @param masterkey
|
|
67
|
+
* @param store
|
|
68
|
+
* @returns
|
|
69
|
+
*/
|
|
70
|
+
exchangeAPIToken(username_1, masterkey_1) {
|
|
71
|
+
return __awaiter(this, arguments, void 0, function* (username, masterkey, store = false) {
|
|
72
|
+
this.clientOptions.auth = 'bearer ' + masterkey;
|
|
73
|
+
let res = null;
|
|
74
|
+
try {
|
|
75
|
+
res = yield this.request('POST', `users/${username}/apitoken`);
|
|
76
|
+
return res;
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
throw err;
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
if (store && !!res)
|
|
83
|
+
this.clientOptions.auth = 'bearer ' + res.token;
|
|
84
|
+
else
|
|
85
|
+
this.clientOptions.auth = null;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Deletes the user wtih the passed username if the passed
|
|
91
|
+
* masterkey matches the account.
|
|
92
|
+
*
|
|
93
|
+
* @param username
|
|
94
|
+
* @param masterkey
|
|
95
|
+
* @returns
|
|
96
|
+
*/
|
|
97
|
+
deleteAccount(username, masterkey) {
|
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
this.clientOptions.auth = 'bearer ' + masterkey;
|
|
100
|
+
let res = null;
|
|
101
|
+
try {
|
|
102
|
+
res = yield this.request('DELETE', `users/${username}`);
|
|
103
|
+
return res;
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
throw err;
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
this.clientOptions.auth = null;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Returns a list of snippets belonging to the
|
|
115
|
+
* authorized account.
|
|
116
|
+
*
|
|
117
|
+
* This endpoijt must be authorized with an API
|
|
118
|
+
* token.
|
|
119
|
+
*
|
|
120
|
+
* @returns
|
|
121
|
+
*/
|
|
122
|
+
list() {
|
|
123
|
+
return this.request('GET', 'snippets');
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Reqeusts a sinlie snippet by ident or id.
|
|
127
|
+
* @param ident
|
|
128
|
+
* @returns
|
|
129
|
+
*/
|
|
130
|
+
get(ident) {
|
|
131
|
+
return this.request('GET', `snippets/${ident}`);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Creates a new snippet with the passed content.
|
|
135
|
+
* @param snippet
|
|
136
|
+
* @returns
|
|
137
|
+
*/
|
|
138
|
+
create(snippet) {
|
|
139
|
+
return this.request('POST', 'snippets', snippet);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Updates a snippet with the passed content.
|
|
143
|
+
* @param snippet
|
|
144
|
+
* @returns
|
|
145
|
+
*/
|
|
146
|
+
update(snippet) {
|
|
147
|
+
return this.request('POST', `snippets/${snippet.ident}`, snippet);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Deletes a snipped by its ident or ID.
|
|
151
|
+
*
|
|
152
|
+
* This endpoint required authorization by the owner of
|
|
153
|
+
* the snippet. If the snippet was created anonymously,
|
|
154
|
+
* it can not be deleted!
|
|
155
|
+
*
|
|
156
|
+
* @param ident
|
|
157
|
+
* @returns
|
|
158
|
+
*/
|
|
159
|
+
delete(ident) {
|
|
160
|
+
return this.request('DELETE', `snippets/${ident}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
exports.SnippetsClient = SnippetsClient;
|