Skip to content

Commit

Permalink
Merge branch 'master' of github.com:efstajas/tela
Browse files Browse the repository at this point in the history
  • Loading branch information
efstajas committed Apr 26, 2020
2 parents 6f14157 + ad84563 commit c153038
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,29 @@ export default class ExampleApp implements App {

If you need to read information that Intercom sends with the requests, worry not: The first argument for your handler is Intercom's request `body`.

#### 🗃 Returning stored_data and content_url

If you need to send Intercom stored data values and / or a content URL for Live Canvasses in addition to components to construct a view, you can return the more verbose `HandlerResult` or a Promise resolving to a `HandlerResult` instead:

```ts
//your-app.ts
import { App, HandlerResult } from '@efstajas/tela'

export default class ExampleApp implements App {
public initialize = async (body): HandlerResult => {
return {
components: [ /* Your view… */ ],
storedData: {
foo: 'bar'
},
contentUrl: '' // Your Live Canvas Content URL
}
}
}
```

`components` is of course required, while `storedData` and `contentUrl` are optional.

#### 🌍 Handler Context

Alongside the request `body` passed from Intercom, your handler also receives a `context` object as the second argument. The context includes the current app name your handler is running in, the app's base endpoint path along with two objects `hooks` and `methods`, which you can use to find out the app's other handler's endpoints at runtime.
Expand Down
9 changes: 8 additions & 1 deletion src/app.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ export type Handler = (
export type CanvasHandler = (
body: object,
context: HandlerContext
) => Promise<Component[]> | Component[]
) => Promise<Component[] | HandlerResult> | Component[] | HandlerResult

export interface HandlerResult {
components: Component[],
storedData?: {
[key: string]: string
},
contentUrl?: string
}
export interface App {
initialize: CanvasHandler,
submit?: CanvasHandler,
Expand Down
15 changes: 13 additions & 2 deletions src/call-app-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ export default (
app
))

const components = await Promise.resolve(handlerResult)
const result = await Promise.resolve(handlerResult)

if (Array.isArray(result)) {
res.send(buildCanvas(result))
} else {
res.send({
content: {
components: result.components
},
stored_data: result.storedData,
content_url: result.contentUrl
})
}

res.send(buildCanvas(components))
next()
}

0 comments on commit c153038

Please sign in to comment.