1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00

docs: Context api tag (#4117)

### What
Added documentation for context fields and endpoints tagged with the
"Context" tag.

---------

Co-authored-by: Thomas Heartman <thomas@getunleash.ai>
This commit is contained in:
Christopher Kolstad 2023-06-29 14:04:48 +02:00 committed by GitHub
parent a073792d8c
commit b99eafc9cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 90 additions and 17 deletions

View File

@ -94,7 +94,6 @@ const metaRules: Rule[] = [
'batchStaleSchema', 'batchStaleSchema',
'changePasswordSchema', 'changePasswordSchema',
'cloneFeatureSchema', 'cloneFeatureSchema',
'contextFieldSchema',
'createApiTokenSchema', 'createApiTokenSchema',
'createFeatureSchema', 'createFeatureSchema',
'createInvitedUserSchema', 'createInvitedUserSchema',
@ -119,7 +118,6 @@ const metaRules: Rule[] = [
'groupsSchema', 'groupsSchema',
'groupUserModelSchema', 'groupUserModelSchema',
'idSchema', 'idSchema',
'legalValueSchema',
'loginSchema', 'loginSchema',
'maintenanceSchema', 'maintenanceSchema',
'toggleMaintenanceSchema', 'toggleMaintenanceSchema',
@ -187,8 +185,6 @@ const metaRules: Rule[] = [
'batchStaleSchema', 'batchStaleSchema',
'changePasswordSchema', 'changePasswordSchema',
'cloneFeatureSchema', 'cloneFeatureSchema',
'contextFieldSchema',
'contextFieldsSchema',
'createApiTokenSchema', 'createApiTokenSchema',
'createFeatureSchema', 'createFeatureSchema',
'createFeatureStrategySchema', 'createFeatureStrategySchema',
@ -213,7 +209,6 @@ const metaRules: Rule[] = [
'groupsSchema', 'groupsSchema',
'groupUserModelSchema', 'groupUserModelSchema',
'idSchema', 'idSchema',
'legalValueSchema',
'loginSchema', 'loginSchema',
'maintenanceSchema', 'maintenanceSchema',
'toggleMaintenanceSchema', 'toggleMaintenanceSchema',

View File

@ -5,41 +5,59 @@ export const contextFieldSchema = {
$id: '#/components/schemas/contextFieldSchema', $id: '#/components/schemas/contextFieldSchema',
type: 'object', type: 'object',
additionalProperties: false, additionalProperties: false,
description:
'A representation of a [context field](https://docs.getunleash.io/reference/unleash-context).',
required: ['name'], required: ['name'],
properties: { properties: {
name: { name: {
description: 'The name of the context field',
type: 'string', type: 'string',
example: 'userId',
}, },
description: { description: {
description: 'The description of the context field.',
type: 'string', type: 'string',
nullable: true, nullable: true,
example: 'Used to uniquely identify users',
}, },
stickiness: { stickiness: {
description:
'Does this context field support being used for [stickiness](https://docs.getunleash.io/reference/stickiness) calculations',
type: 'boolean', type: 'boolean',
example: true,
}, },
sortOrder: { sortOrder: {
type: 'number', description:
'Used when sorting a list of context fields. Is also used as a tiebreaker if a list of context fields is sorted alphabetically.',
type: 'integer',
example: 900,
}, },
createdAt: { createdAt: {
description: 'When this context field was created',
type: 'string', type: 'string',
format: 'date-time', format: 'date-time',
nullable: true, nullable: true,
example: '2023-06-29T10:19:00.000Z',
}, },
usedInFeatures: { usedInFeatures: {
type: 'number', type: 'integer',
description: description:
'Number of projects where this context field is used in', 'Number of projects where this context field is used in',
example: 3, example: 3,
nullable: true, nullable: true,
minimum: 0,
}, },
usedInProjects: { usedInProjects: {
type: 'number', type: 'integer',
description: description:
'Number of projects where this context field is used in', 'Number of projects where this context field is used in',
example: 2, example: 2,
nullable: true, nullable: true,
minimum: 0,
}, },
legalValues: { legalValues: {
description:
'Allowed values for this context field schema. Can be used to narrow down accepted input',
type: 'array', type: 'array',
items: { items: {
$ref: '#/components/schemas/legalValueSchema', $ref: '#/components/schemas/legalValueSchema',

View File

@ -5,6 +5,7 @@ import { legalValueSchema } from './legal-value-schema';
export const contextFieldsSchema = { export const contextFieldsSchema = {
$id: '#/components/schemas/contextFieldsSchema', $id: '#/components/schemas/contextFieldsSchema',
type: 'array', type: 'array',
description: 'A list of context fields',
items: { items: {
$ref: '#/components/schemas/contextFieldSchema', $ref: '#/components/schemas/contextFieldSchema',
}, },

View File

@ -4,13 +4,19 @@ export const legalValueSchema = {
$id: '#/components/schemas/legalValueSchema', $id: '#/components/schemas/legalValueSchema',
type: 'object', type: 'object',
additionalProperties: false, additionalProperties: false,
description:
'Describes a legal value. Typically used to limit possible values for contextFields or strategy properties',
required: ['value'], required: ['value'],
properties: { properties: {
value: { value: {
description: 'The valid value',
type: 'string', type: 'string',
example: '#c154c1',
}, },
description: { description: {
description: 'Describes this specific legal value',
type: 'string', type: 'string',
example: 'Deep fuchsia',
}, },
}, },
components: {}, components: {},

View File

@ -65,6 +65,9 @@ export class ContextController extends Controller {
middleware: [ middleware: [
openApiService.validPath({ openApiService.validPath({
tags: ['Context'], tags: ['Context'],
summary: 'Gets configured context fields',
description:
'Returns all configured [Context fields](https://docs.getunleash.io/how-to/how-to-define-custom-context-fields) that have been created.',
operationId: 'getContextFields', operationId: 'getContextFields',
responses: { responses: {
200: createResponseSchema('contextFieldsSchema'), 200: createResponseSchema('contextFieldsSchema'),
@ -81,6 +84,9 @@ export class ContextController extends Controller {
middleware: [ middleware: [
openApiService.validPath({ openApiService.validPath({
tags: ['Context'], tags: ['Context'],
summary: 'Gets context field',
description:
'Returns specific [context field](https://docs.getunleash.io/reference/unleash-context) identified by the name in the path',
operationId: 'getContextField', operationId: 'getContextField',
responses: { responses: {
200: createResponseSchema('contextFieldSchema'), 200: createResponseSchema('contextFieldSchema'),
@ -97,6 +103,8 @@ export class ContextController extends Controller {
middleware: [ middleware: [
openApiService.validPath({ openApiService.validPath({
tags: ['Strategies'], tags: ['Strategies'],
summary: 'Get strategies using this context field',
description: `Returns all strategies that use the specified context field. Useful when cleaning up context fields: if this list is empty, it's safe to delete the context field.`,
operationId: 'getStrategiesByContextField', operationId: 'getStrategiesByContextField',
responses: { responses: {
200: createResponseSchema( 200: createResponseSchema(
@ -116,6 +124,9 @@ export class ContextController extends Controller {
openApiService.validPath({ openApiService.validPath({
tags: ['Context'], tags: ['Context'],
operationId: 'createContextField', operationId: 'createContextField',
summary: 'Create a context field',
description:
'Endpoint that allows creation of [custom context fields](https://docs.getunleash.io/reference/unleash-context#custom-context-fields)',
requestBody: createRequestSchema( requestBody: createRequestSchema(
'upsertContextFieldSchema', 'upsertContextFieldSchema',
), ),
@ -136,6 +147,8 @@ export class ContextController extends Controller {
middleware: [ middleware: [
openApiService.validPath({ openApiService.validPath({
tags: ['Context'], tags: ['Context'],
summary: 'Update an existing context field',
description: `Endpoint that allows updating a custom context field. Used to toggle stickiness and add/remove legal values for this context field`,
operationId: 'updateContextField', operationId: 'updateContextField',
requestBody: createRequestSchema( requestBody: createRequestSchema(
'upsertContextFieldSchema', 'upsertContextFieldSchema',
@ -156,6 +169,9 @@ export class ContextController extends Controller {
middleware: [ middleware: [
openApiService.validPath({ openApiService.validPath({
tags: ['Context'], tags: ['Context'],
summary: 'Delete an existing context field',
description:
'Endpoint that allows deletion of a custom context field. Does not validate that context field is not in use, but since context field configuration is stored in a json blob for the strategy, existing strategies are safe.',
operationId: 'deleteContextField', operationId: 'deleteContextField',
responses: { responses: {
200: emptyResponse, 200: emptyResponse,
@ -172,6 +188,9 @@ export class ContextController extends Controller {
middleware: [ middleware: [
openApiService.validPath({ openApiService.validPath({
tags: ['Context'], tags: ['Context'],
summary: 'Validate a context field',
description:
'Check whether the provided data can be used to create a context field. If the data is not valid, ...?',
operationId: 'validate', operationId: 'validate',
requestBody: createRequestSchema('nameSchema'), requestBody: createRequestSchema('nameSchema'),
responses: { responses: {

View File

@ -429,10 +429,10 @@ test(`should not show environment on feature toggle, when environment is disable
.expect(200); .expect(200);
const result = body.environments; const result = body.environments;
const sorted = result.sort((e1, e2) => e1.name[0] < e2.name[0]); let dev = result.find((e) => e.name === 'development');
expect(sorted).toHaveLength(2); expect(dev).toBeTruthy();
expect(sorted[0].name).toBe('development'); expect(dev.enabled).toBe(true);
expect(sorted[0].enabled).toBeTruthy(); let prod = result.find((e) => e.name === 'production');
expect(sorted[1].name).toBe('production'); expect(prod).toBeTruthy();
expect(sorted[1].enabled).toBeFalsy(); expect(prod.enabled).toBe(false);
}); });

View File

@ -1683,42 +1683,56 @@ The provider you choose for your addon dictates what properties the \`parameters
}, },
"contextFieldSchema": { "contextFieldSchema": {
"additionalProperties": false, "additionalProperties": false,
"description": "A representation of a [context field](https://docs.getunleash.io/reference/unleash-context).",
"properties": { "properties": {
"createdAt": { "createdAt": {
"description": "When this context field was created",
"example": "2023-06-29T10:19:00.000Z",
"format": "date-time", "format": "date-time",
"nullable": true, "nullable": true,
"type": "string", "type": "string",
}, },
"description": { "description": {
"description": "The description of the context field.",
"example": "Used to uniquely identify users",
"nullable": true, "nullable": true,
"type": "string", "type": "string",
}, },
"legalValues": { "legalValues": {
"description": "Allowed values for this context field schema. Can be used to narrow down accepted input",
"items": { "items": {
"$ref": "#/components/schemas/legalValueSchema", "$ref": "#/components/schemas/legalValueSchema",
}, },
"type": "array", "type": "array",
}, },
"name": { "name": {
"description": "The name of the context field",
"example": "userId",
"type": "string", "type": "string",
}, },
"sortOrder": { "sortOrder": {
"type": "number", "description": "Used when sorting a list of context fields. Is also used as a tiebreaker if a list of context fields is sorted alphabetically.",
"example": 900,
"type": "integer",
}, },
"stickiness": { "stickiness": {
"description": "Does this context field support being used for [stickiness](https://docs.getunleash.io/reference/stickiness) calculations",
"example": true,
"type": "boolean", "type": "boolean",
}, },
"usedInFeatures": { "usedInFeatures": {
"description": "Number of projects where this context field is used in", "description": "Number of projects where this context field is used in",
"example": 3, "example": 3,
"minimum": 0,
"nullable": true, "nullable": true,
"type": "number", "type": "integer",
}, },
"usedInProjects": { "usedInProjects": {
"description": "Number of projects where this context field is used in", "description": "Number of projects where this context field is used in",
"example": 2, "example": 2,
"minimum": 0,
"nullable": true, "nullable": true,
"type": "number", "type": "integer",
}, },
}, },
"required": [ "required": [
@ -1774,6 +1788,7 @@ The provider you choose for your addon dictates what properties the \`parameters
"type": "object", "type": "object",
}, },
"contextFieldsSchema": { "contextFieldsSchema": {
"description": "A list of context fields",
"items": { "items": {
"$ref": "#/components/schemas/contextFieldSchema", "$ref": "#/components/schemas/contextFieldSchema",
}, },
@ -3468,11 +3483,16 @@ The provider you choose for your addon dictates what properties the \`parameters
}, },
"legalValueSchema": { "legalValueSchema": {
"additionalProperties": false, "additionalProperties": false,
"description": "Describes a legal value. Typically used to limit possible values for contextFields or strategy properties",
"properties": { "properties": {
"description": { "description": {
"description": "Describes this specific legal value",
"example": "Deep fuchsia",
"type": "string", "type": "string",
}, },
"value": { "value": {
"description": "The valid value",
"example": "#c154c1",
"type": "string", "type": "string",
}, },
}, },
@ -7042,6 +7062,7 @@ Note: passing \`null\` as a value for the description property will set it to an
}, },
"/api/admin/context": { "/api/admin/context": {
"get": { "get": {
"description": "Returns all configured [Context fields](https://docs.getunleash.io/how-to/how-to-define-custom-context-fields) that have been created.",
"operationId": "getContextFields", "operationId": "getContextFields",
"responses": { "responses": {
"200": { "200": {
@ -7055,11 +7076,13 @@ Note: passing \`null\` as a value for the description property will set it to an
"description": "contextFieldsSchema", "description": "contextFieldsSchema",
}, },
}, },
"summary": "Gets configured context fields",
"tags": [ "tags": [
"Context", "Context",
], ],
}, },
"post": { "post": {
"description": "Endpoint that allows creation of [custom context fields](https://docs.getunleash.io/reference/unleash-context#custom-context-fields)",
"operationId": "createContextField", "operationId": "createContextField",
"requestBody": { "requestBody": {
"content": { "content": {
@ -7093,6 +7116,7 @@ Note: passing \`null\` as a value for the description property will set it to an
}, },
}, },
}, },
"summary": "Create a context field",
"tags": [ "tags": [
"Context", "Context",
], ],
@ -7100,6 +7124,7 @@ Note: passing \`null\` as a value for the description property will set it to an
}, },
"/api/admin/context/validate": { "/api/admin/context/validate": {
"post": { "post": {
"description": "Check whether the provided data can be used to create a context field. If the data is not valid, ...?",
"operationId": "validate", "operationId": "validate",
"requestBody": { "requestBody": {
"content": { "content": {
@ -7117,6 +7142,7 @@ Note: passing \`null\` as a value for the description property will set it to an
"description": "This response has no body.", "description": "This response has no body.",
}, },
}, },
"summary": "Validate a context field",
"tags": [ "tags": [
"Context", "Context",
], ],
@ -7124,6 +7150,7 @@ Note: passing \`null\` as a value for the description property will set it to an
}, },
"/api/admin/context/{contextField}": { "/api/admin/context/{contextField}": {
"delete": { "delete": {
"description": "Endpoint that allows deletion of a custom context field. Does not validate that context field is not in use, but since context field configuration is stored in a json blob for the strategy, existing strategies are safe.",
"operationId": "deleteContextField", "operationId": "deleteContextField",
"parameters": [ "parameters": [
{ {
@ -7140,11 +7167,13 @@ Note: passing \`null\` as a value for the description property will set it to an
"description": "This response has no body.", "description": "This response has no body.",
}, },
}, },
"summary": "Delete an existing context field",
"tags": [ "tags": [
"Context", "Context",
], ],
}, },
"get": { "get": {
"description": "Returns specific [context field](https://docs.getunleash.io/reference/unleash-context) identified by the name in the path",
"operationId": "getContextField", "operationId": "getContextField",
"parameters": [ "parameters": [
{ {
@ -7168,11 +7197,13 @@ Note: passing \`null\` as a value for the description property will set it to an
"description": "contextFieldSchema", "description": "contextFieldSchema",
}, },
}, },
"summary": "Gets context field",
"tags": [ "tags": [
"Context", "Context",
], ],
}, },
"put": { "put": {
"description": "Endpoint that allows updating a custom context field. Used to toggle stickiness and add/remove legal values for this context field",
"operationId": "updateContextField", "operationId": "updateContextField",
"parameters": [ "parameters": [
{ {
@ -7200,6 +7231,7 @@ Note: passing \`null\` as a value for the description property will set it to an
"description": "This response has no body.", "description": "This response has no body.",
}, },
}, },
"summary": "Update an existing context field",
"tags": [ "tags": [
"Context", "Context",
], ],
@ -7207,6 +7239,7 @@ Note: passing \`null\` as a value for the description property will set it to an
}, },
"/api/admin/context/{contextField}/strategies": { "/api/admin/context/{contextField}/strategies": {
"get": { "get": {
"description": "Returns all strategies that use the specified context field. Useful when cleaning up context fields: if this list is empty, it's safe to delete the context field.",
"operationId": "getStrategiesByContextField", "operationId": "getStrategiesByContextField",
"parameters": [ "parameters": [
{ {
@ -7230,6 +7263,7 @@ Note: passing \`null\` as a value for the description property will set it to an
"description": "contextFieldStrategiesSchema", "description": "contextFieldStrategiesSchema",
}, },
}, },
"summary": "Get strategies using this context field",
"tags": [ "tags": [
"Strategies", "Strategies",
], ],