Skip to main content
edited tags
Link
Glorfindel
  • 6.8k
  • 3
  • 21
  • 47
edited tags
Link
rene
  • 2.7k
  • 2
  • 17
  • 35
bumped to v3.4.0
Source Link
import {
    CommonWrapperObject,
    Errors,
    Filters,
    Tag,
} from "@userscripters/stackexchange-api-types";

export const getTagsAPI = async (search: string): Promise<Tag[]> => {
    const filter: Filters.BuiltIn = "default";

    const uri = new URL(`https://api.stackexchange.com/2.3/tags`);
    uri.search = new URLSearchParams({
        site: location.hostname,
        inname: search.toLowerCase(),
        filter,
    }).toString();

    const res = await fetch(uri.toString());
    if (!res.ok) return [];

    const body = await res.json();

    const { items, error_name } = body as CommonWrapperObject<Tag>;

    const errorMap: Map<Errors.Name, string> = new Map([
        ["bad_parameter", "Invalid parameter"],
        ["access_denied", "The API refused access"],
    ]);

    console.log(errorMap.get(error_name as Errors.Name));

    return items;
};
Version Changes
0.3.0 Deduplicated generated interfaces & exported types globally as StackExchangeAPI namespace
1.0.0 Fixed type date from the API being incorrectly interpreted as Date (it is a number)
2.0.0 "may be absent" fields are now considered optional interface members. This makes the types more accurate but might be a breaking change for your codebase
3.1.1 Common Wrapper Object that the API sends the responses wrapped into is added to the list of generated types. The interface accepts a generic type parameter for the items array values
3.2.4 Built-in filters (e.g. default or total) string union grouped under Filters namespace
3.3.0 API errors as Name and Code unions grouped under Errors namespace
3.4.0API error types are propagated to the common wrapper object fields error_name and error_id
import {
    CommonWrapperObject,
    Errors,
    Filters,
    Tag,
} from "@userscripters/stackexchange-api-types";

export const getTagsAPI = async (search: string): Promise<Tag[]> => {
    const filter: Filters.BuiltIn = "default";

    const uri = new URL(`https://api.stackexchange.com/2.3/tags`);
    uri.search = new URLSearchParams({
        site: location.hostname,
        inname: search.toLowerCase(),
        filter,
    }).toString();

    const res = await fetch(uri.toString());
    if (!res.ok) return [];

    const body = await res.json();

    const { items, error_name } = body as CommonWrapperObject<Tag>;

    const errorMap: Map<Errors.Name, string> = new Map([
        ["bad_parameter", "Invalid parameter"],
        ["access_denied", "The API refused access"],
    ]);

    console.log(errorMap.get(error_name as Errors.Name));

    return items;
};
Version Changes
0.3.0 Deduplicated generated interfaces & exported types globally as StackExchangeAPI namespace
1.0.0 Fixed type date from the API being incorrectly interpreted as Date (it is a number)
2.0.0 "may be absent" fields are now considered optional interface members. This makes the types more accurate but might be a breaking change for your codebase
3.1.1 Common Wrapper Object that the API sends the responses wrapped into is added to the list of generated types. The interface accepts a generic type parameter for the items array values
3.2.4 Built-in filters (e.g. default or total) string union grouped under Filters namespace
3.3.0 API errors as Name and Code unions grouped under Errors namespace
import {
    CommonWrapperObject,
    Errors,
    Filters,
    Tag,
} from "@userscripters/stackexchange-api-types";

export const getTagsAPI = async (search: string): Promise<Tag[]> => {
    const filter: Filters.BuiltIn = "default";

    const uri = new URL(`https://api.stackexchange.com/2.3/tags`);
    uri.search = new URLSearchParams({
        site: location.hostname,
        inname: search.toLowerCase(),
        filter,
    }).toString();

    const res = await fetch(uri.toString());
    if (!res.ok) return [];

    const body = await res.json();

    const { items, error_name } = body as CommonWrapperObject<Tag>;

    const errorMap: Map<Errors.Name, string> = new Map([
        ["bad_parameter", "Invalid parameter"],
        ["access_denied", "The API refused access"],
    ]);

    console.log(errorMap.get(error_name));

    return items;
};
Version Changes
0.3.0 Deduplicated generated interfaces & exported types globally as StackExchangeAPI namespace
1.0.0 Fixed type date from the API being incorrectly interpreted as Date (it is a number)
2.0.0 "may be absent" fields are now considered optional interface members. This makes the types more accurate but might be a breaking change for your codebase
3.1.1 Common Wrapper Object that the API sends the responses wrapped into is added to the list of generated types. The interface accepts a generic type parameter for the items array values
3.2.4 Built-in filters (e.g. default or total) string union grouped under Filters namespace
3.3.0 API errors as Name and Code unions grouped under Errors namespace
3.4.0API error types are propagated to the common wrapper object fields error_name and error_id
bumped to v3.3.0
Source Link
Loading
bumped to v3.2.4
Source Link
Loading
added usage example
Source Link
Loading
bumped to v3.1.1
Source Link
Loading
bumped to v2.0.0
Source Link
Loading
bumped to v1.0.0
Source Link
Loading
bumped to v0.3.0
Source Link
Loading
Source Link
Loading