| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import axios from 'axios';
- import urljoin from 'url-join';
- import { RespondBodyForResponseUrl } from '../interfaces/response-url';
- import { IRespondUtil } from '../interfaces/respond-util';
- type AxiosOptions = {
- headers?: {
- [header:string]: string,
- }
- }
- function getResponseUrlForProxy(proxyUri: string, responseUrl: string): string {
- return urljoin(proxyUri, `/g2s/respond?response_url=${responseUrl}`);
- }
- function getUrl(responseUrl: string, proxyUri: string | null): string {
- return proxyUri == null ? responseUrl : getResponseUrlForProxy(proxyUri, responseUrl);
- }
- export class RespondUtil implements IRespondUtil {
- url!: string;
- options!: AxiosOptions;
- constructor(responseUrl: string, proxyUri: string | null, appSiteUrl: string) {
- this.url = getUrl(responseUrl, proxyUri);
- this.options = {
- headers: {
- 'x-growi-app-site-url': appSiteUrl,
- },
- };
- }
- async respond(body: RespondBodyForResponseUrl): Promise<void> {
- return axios.post(this.url, {
- replace_original: false,
- text: body.text,
- blocks: body.blocks,
- }, this.options);
- }
- async respondInChannel(body: RespondBodyForResponseUrl): Promise<void> {
- return axios.post(this.url, {
- response_type: 'in_channel',
- replace_original: false,
- text: body.text,
- blocks: body.blocks,
- }, this.options);
- }
- async replaceOriginal(body: RespondBodyForResponseUrl): Promise<void> {
- return axios.post(this.url, {
- replace_original: true,
- text: body.text,
- blocks: body.blocks,
- }, this.options);
- }
- async deleteOriginal(): Promise<void> {
- return axios.post(this.url, {
- delete_original: true,
- }, this.options);
- }
- }
- export function generateRespondUtil(responseUrl: string, proxyUri: string | null, appSiteUrl: string): RespondUtil {
- return new RespondUtil(responseUrl, proxyUri, appSiteUrl);
- }
|