-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add more template files to app actions and remove deploy scrip…
…ts from frontend
- Loading branch information
1 parent
4ecd181
commit dbc16a7
Showing
18 changed files
with
270 additions
and
14 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,7 @@ | ||
recursive: true | ||
reporter: 'spec' | ||
require: | ||
- 'ts-node/register' | ||
- './test/hooks.ts' | ||
spec: | ||
- 'src/**/*.spec.ts' # the positional arguments! |
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 @@ | ||
lts/hydrogen |
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 @@ | ||
# SAP App Actions |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
43 changes: 43 additions & 0 deletions
43
apps/sap-commerce-cloud/app-actions/src/actions/testHandler.spec.ts
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,43 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import { makeMockAppActionCallContext } from '../../test/mocks'; | ||
import { AppInstallationProps, SysLink } from 'contentful-management'; | ||
import { AppActionCallContext } from '@contentful/node-apps-toolkit'; | ||
import { handler } from './testHandler'; | ||
|
||
describe('testHandler.handler', () => { | ||
let cmaRequestStub: sinon.SinonStub; | ||
let context: AppActionCallContext; | ||
|
||
const cmaClientMockResponses: [AppInstallationProps] = [ | ||
{ | ||
sys: { | ||
type: 'AppInstallation', | ||
appDefinition: {} as SysLink, | ||
environment: {} as SysLink, | ||
space: {} as SysLink, | ||
version: 1, | ||
createdAt: 'createdAt', | ||
updatedAt: 'updatedAt', | ||
}, | ||
parameters: { | ||
sapApiEndpoint: 'sap-endpoint', | ||
apiKey: 'sap-api-key', | ||
}, | ||
}, | ||
]; | ||
|
||
beforeEach(() => { | ||
cmaRequestStub = sinon.stub(); | ||
context = makeMockAppActionCallContext(cmaClientMockResponses, cmaRequestStub); | ||
}); | ||
|
||
it('returns the base sites result', async () => { | ||
const result = await handler( | ||
{ sapApiEndpoint: 'sap-endpoint', apiKey: 'sap-api-key' }, | ||
context | ||
); | ||
|
||
expect(result).to.have.property('ok', true); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
apps/sap-commerce-cloud/app-actions/src/actions/testHandler.ts
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,17 @@ | ||
import { AppActionCallContext } from '@contentful/node-apps-toolkit'; | ||
import { AppActionCallResponse, BaseSites } from '../types'; | ||
import { withAsyncAppActionErrorHandling } from '../helpers/error-handling'; | ||
|
||
interface AppActionCallParameters {} | ||
|
||
export const handler = withAsyncAppActionErrorHandling( | ||
async ( | ||
_payload: AppActionCallParameters, | ||
_context: AppActionCallContext | ||
): Promise<AppActionCallResponse<BaseSites>> => { | ||
return { | ||
ok: true, | ||
data: [{ channel: 'B2B', name: 'Fashion Site', uid: 'fashion-spa' }], | ||
}; | ||
} | ||
); |
27 changes: 27 additions & 0 deletions
27
apps/sap-commerce-cloud/app-actions/src/helpers/error-handling.spec.ts
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,27 @@ | ||
import { expect } from 'chai'; | ||
import { withAsyncAppActionErrorHandling } from './error-handling'; | ||
import { AppActionCallResponse } from '../types'; | ||
|
||
describe('withAsyncAppActionErrorHandling', () => { | ||
const testHandler = async (arg: string): Promise<AppActionCallResponse<string>> => { | ||
return { ok: true, data: arg }; | ||
}; | ||
|
||
it('returns a function that continues to work as normal with no errors', async () => { | ||
const newFunc = withAsyncAppActionErrorHandling(testHandler); | ||
const result = await newFunc('hello'); | ||
expect(result).to.deep.equal({ ok: true, data: 'hello' }); | ||
}); | ||
|
||
describe('when handler throws an error', () => { | ||
const testHandler = async (_arg: string): Promise<AppActionCallResponse<string>> => { | ||
throw new TypeError('boom!'); | ||
}; | ||
|
||
it('returns a function that returns an error result without throwing', async () => { | ||
const newFunc = withAsyncAppActionErrorHandling(testHandler); | ||
const result = await newFunc('hello'); | ||
expect(result).to.deep.equal({ ok: false, error: { message: 'boom!', type: 'TypeError' } }); | ||
}); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
apps/sap-commerce-cloud/app-actions/src/helpers/error-handling.ts
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,42 @@ | ||
import { AppActionCallResponse, AppActionCallResponseError } from '../types'; | ||
|
||
// for the purposes of the error handler, a HandlerFunction is one that can take any arguments and return any value -- provided | ||
// it _can_ return an AppActionCallResponseError | ||
type HandlerFunction<TFnReturn, TFnArgs extends unknown[]> = ( | ||
...args: TFnArgs | ||
) => Promise<TFnReturn | AppActionCallResponseError>; | ||
|
||
// function wrapper intended of handlers (but really any function that returns our AppActionCallResponse type) | ||
export const withAsyncAppActionErrorHandling = < | ||
TResponseType, | ||
TFnReturn extends AppActionCallResponse<TResponseType>, | ||
TFnArgs extends unknown[] | ||
>( | ||
fn: HandlerFunction<TFnReturn, TFnArgs> | ||
): HandlerFunction<TFnReturn, TFnArgs> => { | ||
const wrappedHandler: HandlerFunction<TFnReturn, TFnArgs> = async (...args) => { | ||
try { | ||
return await fn(...args); | ||
} catch (e) { | ||
// this is mostly for typescript, to handle the case where a non-Error object gets thrown | ||
if (!(e instanceof Error)) { | ||
return { | ||
ok: false, | ||
error: { | ||
message: JSON.stringify(e), | ||
type: 'UnknownError', | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
ok: false, | ||
error: { | ||
type: e.constructor.name, | ||
message: e.message, | ||
}, | ||
}; | ||
} | ||
}; | ||
return wrappedHandler; | ||
}; |
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,7 @@ | ||
import sinon from 'sinon'; | ||
|
||
export const mochaHooks = { | ||
afterEach() { | ||
sinon.restore(); | ||
}, | ||
}; |
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,40 @@ | ||
import { AppActionCallContext } from '@contentful/node-apps-toolkit'; | ||
import { Adapter, PlainClientAPI, createClient } from 'contentful-management'; | ||
import sinon from 'sinon'; | ||
|
||
export const makeMockPlainClient = (responses: any[], stub: sinon.SinonStub): PlainClientAPI => { | ||
for (const [callNumber, response] of responses.entries()) { | ||
stub.onCall(callNumber).returns(response); | ||
} | ||
const apiAdapter: Adapter = { | ||
makeRequest: async <T>(args: T) => { | ||
return stub(args); | ||
}, | ||
}; | ||
return createClient({ apiAdapter }, { type: 'plain' }); | ||
}; | ||
|
||
export const makeMockFetchResponse = ( | ||
body: object, | ||
headers: Record<string, string> = {} | ||
): Response => { | ||
const responseBody = JSON.stringify(body); | ||
return new Response(responseBody, { headers }); | ||
}; | ||
|
||
export const makeMockAppActionCallContext = ( | ||
responses: any[], | ||
cmaStub = sinon.stub() | ||
): AppActionCallContext => { | ||
return { | ||
cma: makeMockPlainClient(responses, cmaStub), | ||
appActionCallContext: { | ||
spaceId: 'space-id', | ||
environmentId: 'environment-id', | ||
appInstallationId: 'app-installation-id', | ||
userId: 'user-id', | ||
cmaHost: 'api.contentful.com', | ||
uploadHost: 'upload.contentful.com', | ||
}, | ||
}; | ||
}; |
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,10 @@ | ||
{ | ||
"extends": "@tsconfig/node18/tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./build", | ||
"sourceMap": false, | ||
"target": "es2022" | ||
}, | ||
"include": ["src"], | ||
"exclude": ["node_modules"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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