UNPKG

@coursebuilder/core

Version:

Core package for Course Builder

1 lines 2.38 kB
{"version":3,"sources":["../../src/providers/partykit.ts"],"sourcesContent":["export interface PartyProviderConfig {\n\tid: string\n\tname: string\n\ttype: 'party'\n\toptions: PartyProviderConsumerConfig\n\tpartyUrlBase: string\n\tbroadcastMessage: (options: BroadcastMessageOptions) => Promise<string>\n}\n\nexport type PartyProviderConsumerConfig = Omit<\n\tPartial<PartyProviderConfig>,\n\t'options' | 'type'\n> & {\n\tpartyUrlBase: string\n}\n\nexport type BroadcastMessageOptions = {\n\tbody: Record<string, any>\n\troomId: string\n}\n\nexport default function PartykitProvider(\n\toptions: PartyProviderConsumerConfig,\n): PartyProviderConfig {\n\treturn {\n\t\tid: 'partykit',\n\t\tname: 'PartyKit',\n\t\ttype: 'party',\n\t\toptions,\n\t\t...options,\n\t\tbroadcastMessage: async (\n\t\t\tbroadcastMessageOptions: BroadcastMessageOptions,\n\t\t) => {\n\t\t\treturn await fetch(\n\t\t\t\t`${options.partyUrlBase}/party/${broadcastMessageOptions.roomId}`,\n\t\t\t\t{\n\t\t\t\t\tmethod: 'POST',\n\t\t\t\t\tbody: JSON.stringify(broadcastMessageOptions.body),\n\t\t\t\t},\n\t\t\t)\n\t\t\t\t.then((res) => {\n\t\t\t\t\treturn res.text()\n\t\t\t\t})\n\t\t\t\t.catch((e) => {\n\t\t\t\t\tconsole.error(e)\n\t\t\t\t\tthrow e\n\t\t\t\t})\n\t\t},\n\t} as const\n}\n\nexport const MockPartykitProvider: PartyProviderConfig = {\n\tid: 'mock-partykit' as const,\n\tname: 'Mock Partykit',\n\ttype: 'party',\n\toptions: {\n\t\tpartyUrlBase: 'mock-callback-url',\n\t},\n\tpartyUrlBase: 'mock-callback-url',\n\tbroadcastMessage: () => Promise.resolve(''),\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAqBA;;;;;;AAAe,SAAf,iBACCA,SAAoC;AAEpC,SAAO;IACNC,IAAI;IACJC,MAAM;IACNC,MAAM;IACNH;IACA,GAAGA;IACHI,kBAAkB,OACjBC,4BAAAA;AAEA,aAAO,MAAMC,MACZ,GAAGN,QAAQO,YAAY,UAAUF,wBAAwBG,MAAM,IAC/D;QACCC,QAAQ;QACRC,MAAMC,KAAKC,UAAUP,wBAAwBK,IAAI;MAClD,CAAA,EAECG,KAAK,CAACC,QAAAA;AACN,eAAOA,IAAIC,KAAI;MAChB,CAAA,EACCC,MAAM,CAACC,MAAAA;AACPC,gBAAQC,MAAMF,CAAAA;AACd,cAAMA;MACP,CAAA;IACF;EACD;AACD;AA5BwBG;AA8BjB,IAAMC,uBAA4C;EACxDpB,IAAI;EACJC,MAAM;EACNC,MAAM;EACNH,SAAS;IACRO,cAAc;EACf;EACAA,cAAc;EACdH,kBAAkB,MAAMkB,QAAQC,QAAQ,EAAA;AACzC;","names":["options","id","name","type","broadcastMessage","broadcastMessageOptions","fetch","partyUrlBase","roomId","method","body","JSON","stringify","then","res","text","catch","e","console","error","PartykitProvider","MockPartykitProvider","Promise","resolve"]}