Редагувати

ServerSecrets Class

A collection of server secrets defined in dedicated server configuration.

Properties

names

read-only names: string[];

A list of available, configured server secrets.

Type: string[]

Methods

get

get(name: string): SecretString | undefined

Returns a SecretString that is a placeholder for a secret configured in a JSON file. In certain objects, like an HttpHeader, this Secret is resolved at the time of execution but is not made available to the script environment.

Parameters

  • name: string

Returns SecretString | undefined

Notes:

  • This function can't be called in restricted-execution mode.
  • This function can be called in early-execution mode.

Examples

getPlayerProfile.ts
import { variables, secrets } from '@minecraft/server-admin';
import { http, HttpRequest, HttpRequestMethod, HttpHeader, HttpResponse } from '@minecraft/server-net';

const serverUrl = variables.get('serverEndpoint');

function getPlayerProfile(playerId: string): Promise<HttpResponse> {
  const req = new HttpRequest(serverUrl + 'getPlayerProfile');

  req.body = JSON.stringify({
    playerId,
  });

  const authTokenSec = secrets.get('authtoken');

  if (!authTokenSec) {
    throw new Error('authtoken secret not defined.');
  }

  req.method = HttpRequestMethod.Post;
  req.headers = [new HttpHeader('Content-Type', 'application/json'), new HttpHeader('auth', authTokenSec)];

  return http.request(req);
}

(preview) Work with this sample on the MCTools.dev code sandbox.