-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added function log entity and plain client - Added unit tests for function log - Added js docstrings
- Loading branch information
1 parent
0486089
commit 9bf52a4
Showing
12 changed files
with
426 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { AxiosInstance } from 'contentful-sdk-core' | ||
import * as raw from './raw' | ||
import type { | ||
CollectionProp, | ||
GetFunctionLogParams, | ||
GetAllFunctionLogParams, | ||
} from '../../../common-types' | ||
import type { RestEndpoint } from '../types' | ||
import type { FunctionLogProps } from '../../../entities/function-log' | ||
|
||
const FunctionLogAlphaHeaders = { | ||
'x-contentful-enable-alpha-feature': 'function-logs', | ||
} | ||
|
||
const baseURL = (params: GetAllFunctionLogParams) => | ||
`/spaces/${params.spaceId}/environments/${params.environmentId}/app_installations/${params.appInstallationId}/functions/${params.functionId}/logs` | ||
|
||
const getURL = (params: GetFunctionLogParams) => | ||
`/spaces/${params.spaceId}/environments/${params.environmentId}/app_installations/${params.appInstallationId}/functions/${params.functionId}/logs/${params.logId}` | ||
|
||
export const get: RestEndpoint<'FunctionLog', 'get'> = ( | ||
http: AxiosInstance, | ||
params: GetFunctionLogParams | ||
) => { | ||
return raw.get<FunctionLogProps>(http, getURL(params), { | ||
headers: { | ||
...FunctionLogAlphaHeaders, | ||
}, | ||
}) | ||
} | ||
|
||
export const getAll: RestEndpoint<'FunctionLog', 'getAll'> = ( | ||
http: AxiosInstance, | ||
params: GetAllFunctionLogParams | ||
) => { | ||
return raw.get<CollectionProp<FunctionLogProps>>(http, baseURL(params), { | ||
headers: { | ||
...FunctionLogAlphaHeaders, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import type { MakeRequest } from './common-types' | ||
import entities from './entities' | ||
import type { FunctionLogProps } from './entities/function-log' | ||
import { wrapFunctionLogCollection } from './entities/function-log' | ||
|
||
/** | ||
* @private | ||
*/ | ||
export type ContentfulFunctionLogApi = ReturnType<typeof createFunctionLogApi> | ||
|
||
/** | ||
* @private | ||
*/ | ||
export default function createFunctionLogApi(makeRequest: MakeRequest) { | ||
const { wrapFunctionLog } = entities.functionLog | ||
|
||
return { | ||
/** | ||
* Get a FunctionLog by it's log ID | ||
* @Param spaceId - Space ID | ||
* @Param environmentId - Environment ID | ||
* @Param appInstallationId - App Installation ID | ||
* @Param functionId - Function ID | ||
* @Param logId - Log ID | ||
* @returns a function log | ||
* ```javascript | ||
* const contentful = require('contentful-management') | ||
* | ||
* const client = contentful.createClient({ | ||
* accessToken: '<content_management_api_key>' | ||
* }) | ||
* | ||
* client.functionLog.get({ | ||
* spaceId: '<space_id>', | ||
* environmentId: '<environment_id>', | ||
* appInstallationId: '<app_installation_id>', | ||
* functionId: '<function_id>', | ||
* logId: '<log_id>' | ||
* }) | ||
* .then((response) => console.log(response.items)) | ||
* .catch(console.error) | ||
* ``` | ||
*/ | ||
get(logId: string) { | ||
const raw = this.toPlainObject() as FunctionLogProps | ||
return makeRequest({ | ||
entityType: 'FunctionLog', | ||
action: 'get', | ||
params: { | ||
spaceId: raw.sys.space.sys.id, | ||
environmentId: raw.sys.environment.sys.id, | ||
appInstallationId: raw.sys.appDefinition.sys.id, | ||
functionId: raw.sys.id, | ||
logId: logId, | ||
}, | ||
}).then((data) => wrapFunctionLog(makeRequest, data)) | ||
}, | ||
|
||
/** | ||
* Get all FunctionLogs | ||
* @Param spaceId - Space ID | ||
* @Param environmentId - Environment ID | ||
* @Param appInstallationId - App Installation ID | ||
* @Param functionId - Function ID | ||
* @returns a collection of FunctionLogs | ||
* ```javascript | ||
* const contentful = require('contentful-management') | ||
* | ||
* const client = contentful.createClient({ | ||
* accessToken: '<content_management_api_key>' | ||
* }) | ||
* | ||
* client.functionLog.getAll({ | ||
* spaceId: '<space_id>', | ||
* environmentId: '<environment_id>', | ||
* appInstallationId: '<app_installation_id>', | ||
* functionId: '<function_id>' | ||
* }) | ||
* .then((response) => console.log(response.items)) | ||
* .catch(console.error) | ||
* ``` | ||
*/ | ||
getAll() { | ||
const raw = this.toPlainObject() as FunctionLogProps | ||
return makeRequest({ | ||
entityType: 'FunctionLog', | ||
action: 'getAll', | ||
params: { | ||
spaceId: raw.sys.space.sys.id, | ||
environmentId: raw.sys.environment.sys.id, | ||
appInstallationId: raw.sys.appDefinition.sys.id, | ||
functionId: raw.sys.id, | ||
}, | ||
}).then((data) => wrapFunctionLogCollection(makeRequest, data)) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { Link, DefaultElements } from '../common-types' | ||
import { freezeSys, toPlainObject } from 'contentful-sdk-core' | ||
import copy from 'fast-copy' | ||
import { wrapCollection } from '../common-utils' | ||
import type { MakeRequest } from '../common-types' | ||
import enhanceWithMethods from '../enhance-with-methods' | ||
import createFunctionLogApi from '../create-function-log-api' | ||
|
||
export type FunctionLogProps = { | ||
sys: { | ||
id: string | ||
type: 'FunctionLog' | ||
createdBy: Link<'User'> // Only users can CRUD | ||
createdAt: string | ||
space: Link<'Space'> | ||
environment: Link<'Environment'> | ||
appDefinition: Link<'AppDefinition'> | ||
} | ||
severity: { | ||
info: number | ||
warn: number | ||
error: number | ||
} | ||
requestId: string | ||
event: { | ||
type: string | ||
query: string | ||
isIntrospectionQuery: boolean | ||
variables: Record<string, any> | ||
} | ||
messages: Array<{ | ||
timestamp: number | ||
type: 'INFO' | 'ERROR' | 'WARN' | ||
message: string | ||
}> | ||
} | ||
|
||
export interface FunctionLog extends FunctionLogProps, DefaultElements<FunctionLogProps> {} | ||
|
||
/** | ||
* @private | ||
* @param makeRequest - function to make requests via an adapter | ||
* @param data - raw contentful-Function data | ||
* @return Wrapped Function data | ||
*/ | ||
export function wrapFunctionLog( | ||
makeRequest: MakeRequest, | ||
data: FunctionLogProps | ||
): FunctionLogProps & ReturnType<typeof createFunctionLogApi> { | ||
const appAction = toPlainObject(copy(data)) | ||
|
||
const appActionWithMethods = enhanceWithMethods(appAction, createFunctionLogApi(makeRequest)) | ||
|
||
return freezeSys(appActionWithMethods) | ||
} | ||
|
||
/** | ||
* @private | ||
* @param makeRequest - function to make requests via an adapter | ||
* @param data - raw contentful-function data | ||
* @return Wrapped App Function collection data | ||
*/ | ||
export const wrapFunctionLogCollection = wrapCollection(wrapFunctionLog) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { | ||
CollectionProp, | ||
GetFunctionLogParams, | ||
GetAllFunctionLogParams, | ||
} from '../../common-types' | ||
import type { FunctionLogProps } from '../../entities/function-log' | ||
import type { OptionalDefaults } from '../wrappers/wrap' | ||
|
||
export type FunctionLogPlainClientAPI = { | ||
/** | ||
* Fetches the specified FunctionLog | ||
* @param params - spaceId, environmentId, appInstallationId, functionId, logId | ||
* @returns the FunctionLog | ||
* @throws if the request fails, or the FunctionLog is not found | ||
* @example | ||
* ```javascript | ||
* const functionLog = await client.functionLog.get({ | ||
* spaceId: '<space_id>', | ||
* environmentId: '<environment_id>', | ||
* appInstallationId: '<app_installation_id>', | ||
* functionId: '<function_id>', | ||
* logId: '<log_id>' | ||
* }); | ||
* ``` | ||
*/ | ||
get(params: OptionalDefaults<GetFunctionLogParams>): Promise<FunctionLogProps> | ||
|
||
/** | ||
* Fetches all FunctionLogs for the given function | ||
* @param params - spaceId, environmentId, appInstallationId, functionId | ||
* @returns an object containing an array of FunctionLogs | ||
* @throws if the request fails, or the FunctionLogs are not found | ||
* @example | ||
* ```javascript | ||
* const functionLogs = await client.functionLog.getAll({ | ||
* spaceId: '<space_id>', | ||
* environmentId: '<environment_id>', | ||
* appInstallationId: '<app_installation_id>', | ||
* functionId: '<function_id>' | ||
* }); | ||
* ``` | ||
*/ | ||
getAll( | ||
params: OptionalDefaults<GetAllFunctionLogParams> | ||
): Promise<CollectionProp<FunctionLogProps>> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.