gis.ph-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Yahaay Labs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,123 @@
1
+ # GIS.PH JavaScript/TypeScript SDK
2
+
3
+ The **`gis.ph-sdk`** package provides a robust, type-safe client for interacting with the `api.gis.ph` service. It simplifies integration by handling authentication, request signing, and error parsing, allowing developers to focus on building features.
4
+
5
+ ## Installation
6
+
7
+ Install the package via your preferred package manager:
8
+
9
+ ```bash
10
+ npm install gis.ph-sdk
11
+ # or
12
+ yarn add gis.ph-sdk
13
+ # or
14
+ pnpm add gis.ph-sdk
15
+ ```
16
+
17
+ ## Getting Started
18
+
19
+ Initialize the client with your API key or access token.
20
+
21
+ ```typescript
22
+ import { GisPh } from 'gis.ph-sdk';
23
+
24
+ const client = new GisPh({
25
+ apiKey: 'YOUR_API_KEY',
26
+ // accessToken: 'YOUR_SUPABASE_ACCESS_TOKEN', // Optional: for user-specific context
27
+ });
28
+ ```
29
+
30
+ ## Resources
31
+
32
+ The SDK organizes API endpoints into logical resources.
33
+
34
+ ### Provinces
35
+
36
+ Interact with Philippine provinces data.
37
+
38
+ #### List Provinces
39
+ Retrieve a paginated list of provinces.
40
+
41
+ ```typescript
42
+ const response = await client.provinces.list({
43
+ page: 1,
44
+ limit: 10
45
+ });
46
+
47
+ console.log(response.data); // Array of Province objects
48
+ ```
49
+
50
+ #### Get Province
51
+ Retrieve details for a specific province by ID.
52
+
53
+ ```typescript
54
+ const { data: province } = await client.provinces.get('PROVINCE_ID');
55
+ ```
56
+
57
+ ### Barangays
58
+
59
+ Interact with barangay data, including search functionality.
60
+
61
+ #### List Barangays
62
+ Filter barangays by province (required) and optionally by municipality or name.
63
+
64
+ ```typescript
65
+ const response = await client.barangays.list({
66
+ province: 'Bohol', // Required: Exact match
67
+ municipality: 'Tubigon', // Optional: Starts-with search
68
+ limit: 20
69
+ });
70
+ ```
71
+
72
+ #### Search Barangays
73
+ Perform a global text search for barangays across all provinces.
74
+
75
+ ```typescript
76
+ const response = await client.barangays.search({
77
+ q: 'Poblacion',
78
+ limit: 5
79
+ });
80
+ ```
81
+
82
+ #### Get Barangay
83
+ Retrieve details for a specific barangay by ID.
84
+
85
+ ```typescript
86
+ const { data: barangay } = await client.barangays.get('BARANGAY_ID');
87
+ ```
88
+
89
+ ## Authentication
90
+
91
+ The SDK supports two authentication methods:
92
+
93
+ 1. **API Key (`apiKey`)**: Best for server-side integrations or public client-side access where allowed. Sets the `X-API-Key` header.
94
+ 2. **Access Token (`accessToken`)**: Used for authenticated user sessions (e.g., Supabase JWTs). Sets the `Authorization: Bearer <token>` header.
95
+
96
+ You can provide one or both in the constructor configuration.
97
+
98
+ ## Error Handling
99
+
100
+ API errors are thrown as `GisPhError` instances, containing status codes and detailed error messages.
101
+
102
+ ```typescript
103
+ import { GisPh, GisPhError } from 'gis.ph-sdk';
104
+
105
+ try {
106
+ await client.barangays.list({ province: '' }); // Missing required param
107
+ } catch (error) {
108
+ if (error instanceof GisPhError) {
109
+ console.error(`API Error ${error.status}: ${error.code} - ${error.message}`);
110
+ // Example: API Error 400: VALIDATION_ERROR - Invalid province
111
+ } else {
112
+ console.error('Network or unexpected error:', error);
113
+ }
114
+ }
115
+ ```
116
+
117
+ ## TypeScript Support
118
+
119
+ The SDK is written in TypeScript and ships with type definitions. Key interfaces like `Province`, `Barangay`, and `ApiResponse` are exported for use in your application.
120
+
121
+ ```typescript
122
+ import type { Province, Barangay } from 'gis.ph-sdk';
123
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ Barangays: () => Barangays,
24
+ GisPh: () => GisPh,
25
+ GisPhError: () => GisPhError,
26
+ Provinces: () => Provinces
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+
30
+ // src/core/errors.ts
31
+ var GisPhError = class extends Error {
32
+ status;
33
+ code;
34
+ issues;
35
+ constructor(message, status, code, issues) {
36
+ super(message);
37
+ this.name = "GisPhError";
38
+ this.status = status;
39
+ this.code = code;
40
+ this.issues = issues;
41
+ }
42
+ };
43
+
44
+ // src/core/fetcher.ts
45
+ var Fetcher = class {
46
+ config;
47
+ constructor(config) {
48
+ this.config = {
49
+ baseUrl: "https://api.gis.ph/v1",
50
+ ...config
51
+ };
52
+ }
53
+ async request(endpoint, options = {}) {
54
+ const url = new URL(`${this.config.baseUrl}${endpoint}`);
55
+ if (options.query) {
56
+ Object.entries(options.query).forEach(([key, value]) => {
57
+ if (value !== void 0 && value !== null) {
58
+ url.searchParams.append(key, String(value));
59
+ }
60
+ });
61
+ }
62
+ const headers = {
63
+ "Content-Type": "application/json",
64
+ ...options.headers
65
+ };
66
+ if (this.config.accessToken) {
67
+ headers["Authorization"] = `Bearer ${this.config.accessToken}`;
68
+ }
69
+ if (this.config.apiKey) {
70
+ headers["X-API-Key"] = this.config.apiKey;
71
+ }
72
+ try {
73
+ const response = await fetch(url.toString(), {
74
+ ...options,
75
+ headers
76
+ });
77
+ let data;
78
+ const contentType = response.headers.get("content-type");
79
+ if (contentType && contentType.includes("application/json")) {
80
+ data = await response.json();
81
+ } else {
82
+ data = await response.text();
83
+ }
84
+ if (!response.ok) {
85
+ throw new GisPhError(
86
+ data?.message || response.statusText,
87
+ response.status,
88
+ data?.error?.code,
89
+ data?.error?.issues
90
+ );
91
+ }
92
+ if (data && typeof data === "object" && "data" in data) {
93
+ return data.data;
94
+ }
95
+ return data;
96
+ } catch (error) {
97
+ if (error instanceof GisPhError) {
98
+ throw error;
99
+ }
100
+ throw new GisPhError(
101
+ error instanceof Error ? error.message : "Unknown network error",
102
+ 0
103
+ );
104
+ }
105
+ }
106
+ };
107
+
108
+ // src/core/base.ts
109
+ var BaseResource = class {
110
+ fetcher;
111
+ constructor(fetcher) {
112
+ this.fetcher = fetcher;
113
+ }
114
+ };
115
+
116
+ // src/resources/barangays.ts
117
+ var Barangays = class extends BaseResource {
118
+ async list(params) {
119
+ return this.fetcher.request("/barangays", {
120
+ query: params
121
+ });
122
+ }
123
+ async get(id) {
124
+ return this.fetcher.request(`/barangays/${id}`);
125
+ }
126
+ async search(params) {
127
+ return this.fetcher.request("/barangays/search", {
128
+ query: params
129
+ });
130
+ }
131
+ };
132
+
133
+ // src/resources/provinces.ts
134
+ var Provinces = class extends BaseResource {
135
+ async list(params = {}) {
136
+ return this.fetcher.request("/provinces", {
137
+ query: params
138
+ });
139
+ }
140
+ async get(id) {
141
+ return this.fetcher.request(`/provinces/${id}`);
142
+ }
143
+ };
144
+
145
+ // src/client.ts
146
+ var GisPh = class {
147
+ barangays;
148
+ provinces;
149
+ fetcher;
150
+ constructor(config = {}) {
151
+ this.fetcher = new Fetcher(config);
152
+ this.barangays = new Barangays(this.fetcher);
153
+ this.provinces = new Provinces(this.fetcher);
154
+ }
155
+ };
156
+ // Annotate the CommonJS export names for ESM import in node:
157
+ 0 && (module.exports = {
158
+ Barangays,
159
+ GisPh,
160
+ GisPhError,
161
+ Provinces
162
+ });
@@ -0,0 +1,94 @@
1
+ interface GisPhConfig {
2
+ /**
3
+ * API Key for server-side or authorized access.
4
+ */
5
+ apiKey?: string;
6
+ /**
7
+ * Supabase access token for client-side user context.
8
+ */
9
+ accessToken?: string;
10
+ /**
11
+ * Base URL for the API. Defaults to https://api.gis.ph/v1
12
+ */
13
+ baseUrl?: string;
14
+ }
15
+ interface RequestOptions extends RequestInit {
16
+ query?: Record<string, string | number | undefined | null>;
17
+ }
18
+ interface ApiResponse<T> {
19
+ data: T;
20
+ meta?: {
21
+ total: number;
22
+ page: number;
23
+ limit: number;
24
+ totalPages: number;
25
+ };
26
+ error: null | {
27
+ message: string;
28
+ code?: string;
29
+ issues?: any[];
30
+ };
31
+ }
32
+
33
+ declare class Fetcher {
34
+ private config;
35
+ constructor(config: GisPhConfig);
36
+ request<T>(endpoint: string, options?: RequestOptions): Promise<T>;
37
+ }
38
+
39
+ declare class BaseResource {
40
+ protected fetcher: Fetcher;
41
+ constructor(fetcher: Fetcher);
42
+ }
43
+
44
+ interface Barangay {
45
+ id: string;
46
+ name: string;
47
+ [key: string]: any;
48
+ }
49
+ interface BarangayListParams {
50
+ province: string;
51
+ municipality?: string;
52
+ name?: string;
53
+ page?: number;
54
+ limit?: number;
55
+ }
56
+ interface BarangaySearchParams {
57
+ q: string;
58
+ limit?: number;
59
+ }
60
+ declare class Barangays extends BaseResource {
61
+ list(params: BarangayListParams): Promise<ApiResponse<Barangay[]>>;
62
+ get(id: string): Promise<ApiResponse<Barangay>>;
63
+ search(params: BarangaySearchParams): Promise<ApiResponse<Barangay[]>>;
64
+ }
65
+
66
+ interface Province {
67
+ id: string;
68
+ name: string;
69
+ [key: string]: any;
70
+ }
71
+ interface ProvinceListParams {
72
+ page?: number;
73
+ limit?: number;
74
+ }
75
+ declare class Provinces extends BaseResource {
76
+ list(params?: ProvinceListParams): Promise<ApiResponse<Province[]>>;
77
+ get(id: string): Promise<ApiResponse<Province>>;
78
+ }
79
+
80
+ declare class GisPhError extends Error {
81
+ status: number;
82
+ code?: string | undefined;
83
+ issues?: any[] | undefined;
84
+ constructor(message: string, status: number, code?: string, issues?: any[]);
85
+ }
86
+
87
+ declare class GisPh {
88
+ barangays: Barangays;
89
+ provinces: Provinces;
90
+ private fetcher;
91
+ constructor(config?: GisPhConfig);
92
+ }
93
+
94
+ export { type ApiResponse, type Barangay, type BarangayListParams, type BarangaySearchParams, Barangays, GisPh, type GisPhConfig, GisPhError, type Province, type ProvinceListParams, Provinces, type RequestOptions };
@@ -0,0 +1,94 @@
1
+ interface GisPhConfig {
2
+ /**
3
+ * API Key for server-side or authorized access.
4
+ */
5
+ apiKey?: string;
6
+ /**
7
+ * Supabase access token for client-side user context.
8
+ */
9
+ accessToken?: string;
10
+ /**
11
+ * Base URL for the API. Defaults to https://api.gis.ph/v1
12
+ */
13
+ baseUrl?: string;
14
+ }
15
+ interface RequestOptions extends RequestInit {
16
+ query?: Record<string, string | number | undefined | null>;
17
+ }
18
+ interface ApiResponse<T> {
19
+ data: T;
20
+ meta?: {
21
+ total: number;
22
+ page: number;
23
+ limit: number;
24
+ totalPages: number;
25
+ };
26
+ error: null | {
27
+ message: string;
28
+ code?: string;
29
+ issues?: any[];
30
+ };
31
+ }
32
+
33
+ declare class Fetcher {
34
+ private config;
35
+ constructor(config: GisPhConfig);
36
+ request<T>(endpoint: string, options?: RequestOptions): Promise<T>;
37
+ }
38
+
39
+ declare class BaseResource {
40
+ protected fetcher: Fetcher;
41
+ constructor(fetcher: Fetcher);
42
+ }
43
+
44
+ interface Barangay {
45
+ id: string;
46
+ name: string;
47
+ [key: string]: any;
48
+ }
49
+ interface BarangayListParams {
50
+ province: string;
51
+ municipality?: string;
52
+ name?: string;
53
+ page?: number;
54
+ limit?: number;
55
+ }
56
+ interface BarangaySearchParams {
57
+ q: string;
58
+ limit?: number;
59
+ }
60
+ declare class Barangays extends BaseResource {
61
+ list(params: BarangayListParams): Promise<ApiResponse<Barangay[]>>;
62
+ get(id: string): Promise<ApiResponse<Barangay>>;
63
+ search(params: BarangaySearchParams): Promise<ApiResponse<Barangay[]>>;
64
+ }
65
+
66
+ interface Province {
67
+ id: string;
68
+ name: string;
69
+ [key: string]: any;
70
+ }
71
+ interface ProvinceListParams {
72
+ page?: number;
73
+ limit?: number;
74
+ }
75
+ declare class Provinces extends BaseResource {
76
+ list(params?: ProvinceListParams): Promise<ApiResponse<Province[]>>;
77
+ get(id: string): Promise<ApiResponse<Province>>;
78
+ }
79
+
80
+ declare class GisPhError extends Error {
81
+ status: number;
82
+ code?: string | undefined;
83
+ issues?: any[] | undefined;
84
+ constructor(message: string, status: number, code?: string, issues?: any[]);
85
+ }
86
+
87
+ declare class GisPh {
88
+ barangays: Barangays;
89
+ provinces: Provinces;
90
+ private fetcher;
91
+ constructor(config?: GisPhConfig);
92
+ }
93
+
94
+ export { type ApiResponse, type Barangay, type BarangayListParams, type BarangaySearchParams, Barangays, GisPh, type GisPhConfig, GisPhError, type Province, type ProvinceListParams, Provinces, type RequestOptions };
package/dist/index.js ADDED
@@ -0,0 +1,132 @@
1
+ // src/core/errors.ts
2
+ var GisPhError = class extends Error {
3
+ status;
4
+ code;
5
+ issues;
6
+ constructor(message, status, code, issues) {
7
+ super(message);
8
+ this.name = "GisPhError";
9
+ this.status = status;
10
+ this.code = code;
11
+ this.issues = issues;
12
+ }
13
+ };
14
+
15
+ // src/core/fetcher.ts
16
+ var Fetcher = class {
17
+ config;
18
+ constructor(config) {
19
+ this.config = {
20
+ baseUrl: "https://api.gis.ph/v1",
21
+ ...config
22
+ };
23
+ }
24
+ async request(endpoint, options = {}) {
25
+ const url = new URL(`${this.config.baseUrl}${endpoint}`);
26
+ if (options.query) {
27
+ Object.entries(options.query).forEach(([key, value]) => {
28
+ if (value !== void 0 && value !== null) {
29
+ url.searchParams.append(key, String(value));
30
+ }
31
+ });
32
+ }
33
+ const headers = {
34
+ "Content-Type": "application/json",
35
+ ...options.headers
36
+ };
37
+ if (this.config.accessToken) {
38
+ headers["Authorization"] = `Bearer ${this.config.accessToken}`;
39
+ }
40
+ if (this.config.apiKey) {
41
+ headers["X-API-Key"] = this.config.apiKey;
42
+ }
43
+ try {
44
+ const response = await fetch(url.toString(), {
45
+ ...options,
46
+ headers
47
+ });
48
+ let data;
49
+ const contentType = response.headers.get("content-type");
50
+ if (contentType && contentType.includes("application/json")) {
51
+ data = await response.json();
52
+ } else {
53
+ data = await response.text();
54
+ }
55
+ if (!response.ok) {
56
+ throw new GisPhError(
57
+ data?.message || response.statusText,
58
+ response.status,
59
+ data?.error?.code,
60
+ data?.error?.issues
61
+ );
62
+ }
63
+ if (data && typeof data === "object" && "data" in data) {
64
+ return data.data;
65
+ }
66
+ return data;
67
+ } catch (error) {
68
+ if (error instanceof GisPhError) {
69
+ throw error;
70
+ }
71
+ throw new GisPhError(
72
+ error instanceof Error ? error.message : "Unknown network error",
73
+ 0
74
+ );
75
+ }
76
+ }
77
+ };
78
+
79
+ // src/core/base.ts
80
+ var BaseResource = class {
81
+ fetcher;
82
+ constructor(fetcher) {
83
+ this.fetcher = fetcher;
84
+ }
85
+ };
86
+
87
+ // src/resources/barangays.ts
88
+ var Barangays = class extends BaseResource {
89
+ async list(params) {
90
+ return this.fetcher.request("/barangays", {
91
+ query: params
92
+ });
93
+ }
94
+ async get(id) {
95
+ return this.fetcher.request(`/barangays/${id}`);
96
+ }
97
+ async search(params) {
98
+ return this.fetcher.request("/barangays/search", {
99
+ query: params
100
+ });
101
+ }
102
+ };
103
+
104
+ // src/resources/provinces.ts
105
+ var Provinces = class extends BaseResource {
106
+ async list(params = {}) {
107
+ return this.fetcher.request("/provinces", {
108
+ query: params
109
+ });
110
+ }
111
+ async get(id) {
112
+ return this.fetcher.request(`/provinces/${id}`);
113
+ }
114
+ };
115
+
116
+ // src/client.ts
117
+ var GisPh = class {
118
+ barangays;
119
+ provinces;
120
+ fetcher;
121
+ constructor(config = {}) {
122
+ this.fetcher = new Fetcher(config);
123
+ this.barangays = new Barangays(this.fetcher);
124
+ this.provinces = new Provinces(this.fetcher);
125
+ }
126
+ };
127
+ export {
128
+ Barangays,
129
+ GisPh,
130
+ GisPhError,
131
+ Provinces
132
+ };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "gis.ph-sdk",
3
+ "version": "1.0.0",
4
+ "description": "Official JavaScript/TypeScript SDK for api.gis.ph - Access Philippine geographical data (Provinces, Barangays, LGUs) and more.",
5
+ "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "main": "./dist/index.js",
10
+ "module": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "require": "./dist/index.cjs",
16
+ "import": "./dist/index.js"
17
+ }
18
+ },
19
+ "scripts": {
20
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
21
+ "test": "vitest",
22
+ "prepublishOnly": "npm run build"
23
+ },
24
+ "keywords": [
25
+ "philippines",
26
+ "gis",
27
+ "sdk",
28
+ "api",
29
+ "barangay",
30
+ "province",
31
+ "lgu",
32
+ "regions",
33
+ "typescript"
34
+ ],
35
+ "author": "Gerald Villacarlos <gerald@yahaay.com>",
36
+ "license": "MIT",
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/yahaaylabs/gis.ph-sdk-js.git"
40
+ },
41
+ "bugs": {
42
+ "url": "https://github.com/yahaaylabs/gis.ph-sdk-js/issues"
43
+ },
44
+ "homepage": "https://github.com/yahaaylabs/gis.ph-sdk-js#readme",
45
+ "devDependencies": {
46
+ "tsup": "^8.5.1",
47
+ "typescript": "^5.9.3",
48
+ "vitest": "^4.0.18"
49
+ },
50
+ "dependencies": {
51
+ "zod": "^4.3.6"
52
+ }
53
+ }