so://phie

verbal

A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal

blob: ce1c16b2b6d959160c45f8fa8114e8f555a012e7 (2671 bytes)[doc][blame][raw]

1import {state} from './main.js';

2

3const ltr = (parts, values) => {

4 let string = '';

5 for(let i = 0; i < parts.length; i++) {

6 string += parts[i];

7 string += values[i] ?? '';

8 }

9 return string;

10}

11

12export function http(parts, ...values) {

13 const line = ltr(parts, values).trim();

14 const regex = /^(\w+)::([\w\/.]*)$/;

15 const [, method, path] = line.match(regex) ?? [];

16 return (query) => {

17 query = {...query};

18 return new Promise(async (resolve, reject) => {

19 try {

20 const headers = {

21 'content-type': 'application/json',

22 }

23 const token = localStorage.getItem('verbal-token') ?? 'test-token';

24 if(token) {

25 headers['verbal-token'] = token;

26 }

27 let body = null;

28 let params = '';

29 if(method === 'get' || method === 'head') {

30 if(query !== undefined && query !== null) {

31 for(const [key, value] of Object.entries(query)) {

32 if(Array.isArray(value)) {

33 delete query[key];

34 for(let i = 0; i < value.length; i++) {

35 query[`${key}[${i}]`] = value[i];

36 }

37 }

38 else if(value === null) {

39 delete query[key];

40 }

41 }

42 }

43 params = '?' + new URLSearchParams(query).toString();

44 params = params.replaceAll(/%5B/g, '[');

45 params = params.replaceAll(/%5D/g, ']');

46 }

47 else {

48 body = JSON.stringify(query);

49 }

50 const response = await fetch(window.location.origin + path + params, {

51 method,

52 headers,

53 body

54 });

55 const json = await response.json();

56 const {ok, data, error} = json;

57 if(ok === undefined) {

58 resolve(json);

59 return;

60 }

61 if(!ok) {

62 reject(error.message);

63 return;

64 }

65 resolve(data);

66 }

67 catch(error) {

68 reject(error);

69 }

70 });

71 };

72}

73

74export async function update() {

75

76}

77

78export function $lang(id) {

79 return state.locale[state.account?.language ?? 'en'][id];

80}