masto: add fetchStatus
* also add types RemoteServer and AccountKey
This commit is contained in:
		
							parent
							
								
									97bd6da9ac
								
							
						
					
					
						commit
						5ab0d4d0a2
					
				
					 3 changed files with 68 additions and 2 deletions
				
			
		
							
								
								
									
										23
									
								
								src/masto/base.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/masto/base.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,23 @@
 | 
			
		|||
import { createCacheBucket } from "~platform/cache";
 | 
			
		||||
 | 
			
		||||
export const cacheBucket = /* @__PURE__ */ createCacheBucket("mastodon");
 | 
			
		||||
 | 
			
		||||
export function toSmallCamelCase<T>(object: T) :T {
 | 
			
		||||
  if (!object || typeof object !== "object") {
 | 
			
		||||
    return object;
 | 
			
		||||
  } else if (Array.isArray(object)) {
 | 
			
		||||
    return object.map(toSmallCamelCase) as T
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  const result = {} as Record<keyof any, unknown>;
 | 
			
		||||
  for (const k in object) {
 | 
			
		||||
    const value = toSmallCamelCase(object[k])
 | 
			
		||||
    const nk =
 | 
			
		||||
      typeof k === "string"
 | 
			
		||||
        ? k.replace(/_(.)/g, (_, match) => match.toUpperCase())
 | 
			
		||||
        : k;
 | 
			
		||||
    result[nk] = value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return result as T;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								src/masto/statuses.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/masto/statuses.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,30 @@
 | 
			
		|||
import { CachedFetch } from "~platform/cache";
 | 
			
		||||
import { cacheBucket, toSmallCamelCase } from "./base";
 | 
			
		||||
import {
 | 
			
		||||
  isAccountKey,
 | 
			
		||||
  type RemoteServer,
 | 
			
		||||
} from "../accounts/stores";
 | 
			
		||||
import type { mastodon } from "masto";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
export const fetchStatus = /* @__PURE__ */ new CachedFetch(
 | 
			
		||||
  cacheBucket,
 | 
			
		||||
  (session: RemoteServer, id: string) => {
 | 
			
		||||
    const headers = new Headers({
 | 
			
		||||
      Accept: "application/json",
 | 
			
		||||
    });
 | 
			
		||||
    if (isAccountKey(session)) {
 | 
			
		||||
      headers.set("Authorization", `Bearer ${session.accessToken}`);
 | 
			
		||||
    }
 | 
			
		||||
    return {
 | 
			
		||||
      url: new URL(`./api/v1/statuses/${id}`, session.site).toString(),
 | 
			
		||||
      headers,
 | 
			
		||||
    };
 | 
			
		||||
  },
 | 
			
		||||
  async (response) => {
 | 
			
		||||
    return toSmallCamelCase(
 | 
			
		||||
      await response.json(),
 | 
			
		||||
    ) as unknown as mastodon.v1.Status;
 | 
			
		||||
  },
 | 
			
		||||
);
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue